- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
你好,我最近更新了我的 spring boot 应用程序并注意到了新功能(DirtiesContext.ClassMode.BEFORE_CLASS),这似乎符合我的需要,因为我在使用注释为@RabbitListener 的方法重新加载 bean 时遇到了问题,出于某种原因 Spring 重新加载了那个 bean ,但是老 bean 被用作兔子的听众。 (参见 here)
我的代码:
@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 上下文?
最佳答案
这里的解决办法是加上DependencyInjectionTestExecutionListener列出您的 @TestExecutionListeners
,这将为依赖注入(inject)提供支持。
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 将注入(inject)默认监听器,其中包括 DirtiesContextBeforeModesTestExecutionListener.class
和 DirtiesContextTestExecutionListener.class
这是SpringBoot应用程序没有@TestExecutionListeners
注解的默认监听器列表
关于java - 使用@DirtiesContext BEFORE_CLASS 进行 Spring 启动测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993942/
我一直在使用 Spring,遇到了 spring 提供的 @DirtiesContext 注释。当您想在运行每个方法之前刷新应用程序上下文时(当然以牺牲性能为代价),在每个方法上使用它是有意义的,但是
自从我从 Spring 3.2.6 迁移到 Spring 4.1.2 后,我的测试就无法正常工作了。我发现 AnnotationTransactionAspect (TransactionAspect
我有几个junit测试, @ContextConfiguration(locations = { "file:../business/src/test/resources/application-co
我有一个测试用例,它修改了我的 spring 上下文,并且可能会遇到无限循环。在测试用例之后,我希望有一个干净的 spring 上下文,因此我使用 @DirtiesContext 注释。为了防止无限循
我正在开发一些使用 Kafka-streams 的应用程序的测试,该应用程序还使用 Spring Boot 1.5,它导入 spring-kafka 的 1.2 版。详细来说,我正在使用 KafkaE
由 Cucumber 执行的集成测试往往会留下导致后续测试出现问题的上下文。明显的解决方案似乎是 Spring 的 @DirtiesContext,但它不是在运行所有 cucumber 功能之后拆除上
在 Spring Boot 中,我使用 @DirtiesContext 来确保在每个测试用例之前清除数据库。 Quarkus AFAIK 没有@DirtiesContext 注释。相反,建议使用 @T
我正在尝试在 Spring Boot 中运行一些集成测试。为此,我认为我们应该在每次测试运行后使用 @DirtiesContext 来清理数据库。 我现在尝试了几种方法,但效果都一样。 这是我的设置:
我正在开发一个 Spring-Boot 网络应用程序。编写集成测试的通常方式是: @Test @Transactional @Rollback(true) public void myTest() {
测试方法后我的应用程序上下文未关闭。 我使用 Junit 5.3.1、spring 5.1.0.RELEASE 进行 Selenium WebDriver 测试。 这是我的 bean : @Confi
你好,我最近更新了我的 spring boot 应用程序并注意到了新功能(DirtiesContext.ClassMode.BEFORE_CLASS),这似乎符合我的需要,因为我在使用注释为@Rabb
在我的一个测试类(class)中,我使用: //Make a security context SecurityContext securityContext = mock( SecurityCont
上下文 带有 Rest 端点和 JMS AMQ 监听器的 Spring Boot 应用程序 观察到的测试行为 测试类在不需要单独的 DirtiesContext 的情况下运行良好,但是当整个测试类套件
我正在尝试执行 Spring Boot 应用程序的一组 JUnit4 测试类,该应用程序由多个 Web 服务组成并配置了一个数据库。 每次测试后都会清除上下文,这很方便,因此我在每个测试类上添加了 @
我是一名优秀的程序员,十分优秀!