Is it possible to intercept JUnit test methods?

Viewed 510

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

Is it possible to tell SpringJUnit4ClassRunner to use myTest() instance of the test class? Something like:

@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();
    }
}
0 Answers
Related