我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes ={ServerConfig.class,ServerThroughAMQPBrokerRabbitMQIntegrationTestConfig.class})
@Category({IntegrationTest.class})
@TestPropertySource("classpath:test.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@TestExecutionListeners(listeners = {DirtiesContextBeforeModesTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
public class ServerThroughAMQPBrokerRabbitMQIntegrationTest {
添加DirtiesContext.ClassMode.BEFORE_CLASS Spring之后的问题将停止从以下位置加载bean:
@SpringApplicationConfiguration(classes ={ServerConfig.class,ServerThroughAMQPBrokerRabbitMQIntegrationTestConfig.class})
所以问题:
我应该如何使用DirtiesContext.ClassMode.BEFORE_CLASS加载spring上下文?
TestExecutionListener which provides support for dependency injection
and initialization of test instances.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes ={ServerConfig.class,ServerThroughAMQPBrokerRabbitMQIntegrationTestConfig.class})
@Category({IntegrationTest.class})
@TestPropertySource("classpath:test.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@TestExecutionListeners(listeners = {DirtiesContextBeforeModesTestExecutionListener.class, DirtiesContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
public class ServerThroughAMQPBrokerRabbitMQIntegrationTest {
这是来自DirtiesContextBeforeModesTestExecutionListener javadoc
When merging TestExecutionListeners with the defaults, this listener
will automatically be ordered before the
DependencyInjectionTestExecutionListener; otherwise, this listener
must be manually configured to execute before the
DependencyInjectionTestExecutionListener.
您还可以删除测试类上的@TestExecutionListeners注释.如果没有这样的注释,Spring将注入默认监听器,其中包括DirtiesContextBeforeModesTestExecutionListener.class和DirtiesContextTestExecutionListener.class
以下是没有SpringBoot应用程序的@TestExecutionListeners注释的默认侦听器列表
转载注明原文:java – 使用@DirtiesContext BEFORE_CLASS进行Spring启动测试 - 乐贴网