java – 使用@DirtiesContext BEFORE_CLASS进行Spring启动测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用@DirtiesContext BEFORE_CLASS进行Spring启动测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2438字,纯文字阅读大概需要4分钟。
内容图文
![java – 使用@DirtiesContext BEFORE_CLASS进行Spring启动测试](/upload/InfoBanner/zyjiaocheng/810/4e8c7a17526d453e929eadcfc5643b07.jpg)
您好我最近更新了我的spring启动应用程序并注意到了新功能(DirtiesContext.ClassMode.BEFORE_CLASS),这似乎符合我的需要,因为我遇到了使用方法注释@RabbitListener重新加载bean的问题,出于某种原因Spring重新加载了那个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列表中,这将为依赖项注入提供支持.
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启动测试全部内容,希望文章能够帮你解决java – 使用@DirtiesContext BEFORE_CLASS进行Spring启动测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。