Is it possible to intercept JUnit testing methods?

I would like to intercept JUnit testing methods. I already have an interceptor that prepares the context for the test. I don't want to duplicate code in my interceptor.

Can I tell SpringJUnit4ClassRunner to use the myTest () instance of the test class? Sort of:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyConfig.class })
public class MyTest {

    @Autowired
    private MyInterceptor myInterceptor;

    @Autowired
    private BeanFactory beanFactory;

    @Bean
    public MyTest myTest() {
        final ProxyFactoryBean proxy = new ProxyFactoryBean();
        proxy.setBeanFactory(this.beanFactory);
        proxy.setTarget(this);
        proxy.addAdvice(this.myInterceptor);

        return (MyTest) proxy.getObject();
    }
}
+4
source share

Source: https://habr.com/ru/post/1678266/


All Articles