tproenca
tproenca

Reputation: 146

Disable Spring TestExecutionListeners from inherited class

I have the following scenario:

@TestExecutionListeners(BasicListener.class)
public @interface AnnotationOne {
}

@AnnotationOne
public class TestClassOne extends AbstractJUnit4SpringContextTests {
...
}

And since:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class})
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
...
}

For some reason, the TestExecutionListeners don't get merged. So, is there a way to disable the TestExecutionListeners defined by the parent class?

My main concern is to have the BasicListener working.

Thanks

Upvotes: 2

Views: 1261

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31247

The behavior you are experiencing is due to a bug in core Spring: an inherited annotation will shadow a locally declared composed annotation.

In your use case, @TestExecutionListeners is the inherited annotation, and @AnnotationOne is the locally declared composed annotation.

This bug has been fixed in Spring Framework 4.2 RC1. For details, see SPR-12749.

If you'd like a work-around before 4.2 is released, the following should work for you, but you will not be able to use your custom @AnnotationOne annotation if your class extends from AbstractJUnit4SpringContextTests:

@TestExecutionListeners(listeners = BasicListener.class, inheritListeners = false)
public class TestClassOne extends AbstractJUnit4SpringContextTests {
    // ...
}

Generally speaking, AbstractJUnit4SpringContextTests adds very little value. So you're probably better off with the following approach:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(BasicListener.class)
public class TestClassOne {
    // ...
}

Or even:

@RunWith(SpringJUnit4ClassRunner.class)
@AnnotationOne
public class TestClassOne {
    // ...
}

Regards,

Sam (author of the Spring TestContext Framework)

Upvotes: 1

Related Questions