加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 没有缓存Spring ApplicationContext用于与Maven的集成

发布时间:2020-12-15 01:23:53 所属栏目:大数据 来源:网络整理
导读:当集成测试从IDE启动时,ApllicationContext只加载一次,然后在测试之间共享,并且它按预期工作.但执行mvn clean install每次测试都会创建新的ApllicationContext.根据Spring doc我配置了 maven-failsafe-plugin使用单叉 要缓存ApplicationContext,请使用以下注

当集成测试从IDE启动时,ApllicationContext只加载一次,然后在测试之间共享,并且它按预期工作.但执行mvn clean install每次测试都会创建新的ApllicationContext.根据Spring doc我配置了
maven-failsafe-plugin使用单叉

要缓存ApplicationContext,请使用以下注释:

@ContextConfiguration(classes = TestConfig.class)

使用maven构建应用程序时为什么不共享上下文?
实际上有没有其他方式加速IT测试?谢谢.

更新:

这是多模Maven项目. Accordig Spring IT caching doc

To benefit from the caching mechanism,all tests must run within the
same process or test suite. This can be achieved by executing all
tests as a group within an IDE. Similarly,when executing tests with a
build framework such as Ant,Maven,or Gradle it is important to make
sure that the build framework does not fork between tests. For
example,if the forkMode for the Maven Surefire plug-in is set to
always or pertest,the TestContext framework will not be able to cache
application contexts between test classes and the build process will
run significantly slower as a result.

所以对于maven-failsafe-plugin 2.14,这个配置等于forkMode =一次

最佳答案
您可以尝试使用指定的上下文位置而不是类:

@ContextConfiguration(locations = "classpath:test-context.xml")

Spring按位置属性缓存应用程序上下文,因此如果第二次出现相同的位置,则Spring使用相同的上下文而不是创建新的上下文.

它来自这里:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html#testing-ctx-management

您也可以在这里阅读有关加速单元测试的内容:
http://www.nurkiewicz.com/2010/12/speeding-up-spring-integration-tests.html

UPDATE

您的项目是多模块Maven项目吗?
相应的文档:

The default setting is forkCount=1/reuseForks=true,which means that Surefire creates one new JVM process to execute all tests in one maven module.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读