I know that if I need to use a precise implementation TestExecutionListener
, it will prevent the loading of the default TestExecutionListeners.
If my test class is like
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({MyCustomTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
MyCustomTestExecutionListener
will be the only loaded Listener and it makes my tests execution fail.
When I launch my tests whitout specifying any TestExecutionListener and I dig in Spring's logs, I can find :
getDefaultTestExecutionListenerClassNames :
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]:
[org.springframework.test.context.web.ServletTestExecutionListener,
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,
org.springframework.test.context.support.DependencyInjectionTestExecutionListener,
org.springframework.test.context.support.DirtiesContextTestExecutionListener,
org.springframework.test.context.transaction.TransactionalTestExecutionListener,
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
So if I want to add my TestExecutionListener, I need to specify (in addition of the wanted implementation) all these default TestExectionListeners on my test class :
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
ServletTestExecutionListener.class,
DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class,
MyCustomTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
Is there a way of just adding one (or more) TestExecutionListener, whithout having to explicitly declare each listener from the default configuration nor "overriding" the default ones ?
import org.springframework.test.context.TestExecutionListeners.MergeMode;
@TestExecutionListeners(value = { MyCustomTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
Is there a way of just adding one (or more) TestExecutionListener, whithout having to explicitly declare each listener from the default configuration nor "overriding" the default ones?
Yes, this is possible since Spring Framework 4.1 and is clearly documented both in the Javadoc for @TestExecutionListeners
and in the Merging TestExecutionListeners section of the reference manual. The following example is taken directly from the reference manual.
@ContextConfiguration
@TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
public class MyTest {
// class body...
}
Regards,
Sam (author of the Spring TestContext Framework)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With