添加一个 TestExecutionListener
Adding a TestExecutionListener
我知道如果我需要使用精确的实现 TestExecutionListener
,它会阻止加载默认的 TestExecutionListeners。
如果我的测试 class 像
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({MyCustomTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
MyCustomTestExecutionListener
将是唯一加载的侦听器,它使我的测试执行失败。
当我在不指定任何 TestExecutionListener 的情况下启动我的测试并挖掘 Spring 的日志时,我可以找到:
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]
所以如果我想添加我的 TestExecutionListener,我需要在我的测试中指定(除了想要的实现之外)所有这些默认的 TestExectionListeners 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 {
...
}
有没有一种方法可以只添加一个(或多个)TestExecutionListener,而不必从默认配置或 "overriding" 默认配置中显式声明每个侦听器?
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?
是的,自 Spring Framework 4.1 以来这是可能的,并且在 @TestExecutionListeners
的 Javadoc 和参考手册的 Merging TestExecutionListeners 部分中都有明确的记录。下面的例子直接取自参考手册
@ContextConfiguration
@TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
public class MyTest {
// class body...
}
此致,
Sam(Spring TestContext Framework 的作者)
我知道如果我需要使用精确的实现 TestExecutionListener
,它会阻止加载默认的 TestExecutionListeners。
如果我的测试 class 像
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({MyCustomTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
MyCustomTestExecutionListener
将是唯一加载的侦听器,它使我的测试执行失败。
当我在不指定任何 TestExecutionListener 的情况下启动我的测试并挖掘 Spring 的日志时,我可以找到:
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]
所以如果我想添加我的 TestExecutionListener,我需要在我的测试中指定(除了想要的实现之外)所有这些默认的 TestExectionListeners 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 {
...
}
有没有一种方法可以只添加一个(或多个)TestExecutionListener,而不必从默认配置或 "overriding" 默认配置中显式声明每个侦听器?
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?
是的,自 Spring Framework 4.1 以来这是可能的,并且在 @TestExecutionListeners
的 Javadoc 和参考手册的 Merging TestExecutionListeners 部分中都有明确的记录。下面的例子直接取自参考手册
@ContextConfiguration
@TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
public class MyTest {
// class body...
}
此致,
Sam(Spring TestContext Framework 的作者)