Spring ApplicationContext没有缓存用于与Maven的集成测试

njj*_*nex 7 java spring integration-testing maven

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

<artifactId>maven-failsafe-plugin</artifactId>
 <version>2.12.4</version>
 <configuration>
    <forkCount>1</forkCount>
    <reuseForks>true</reuseForks>
...
Run Code Online (Sandbox Code Playgroud)

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

@ContextConfiguration(classes = TestConfig.class)
Run Code Online (Sandbox Code Playgroud)

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

更新:

这是多模Maven项目.Accordig Spring IT缓存文档

要从缓存机制中受益,所有测试必须在同一进程或测试套件中运行.这可以通过在IDE中作为一个组执行所有测试来实现.类似地,当使用诸如Ant,Maven或Gradle之类的构建框架执行测试时,确保构建框架不在测试之间进行分配是很重要的.例如,如果Maven Surefire插件的forkMode设置为always或pertest,则TestContext框架将无法在测试类之间缓存应用程序上下文,因此构建过程将显着减慢运行速度.

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

m.a*_*bin 2

您可以尝试指定上下文位置而不是类:

@ContextConfiguration(locations = "classpath:test-context.xml")
Run Code Online (Sandbox Code Playgroud)

Spring 按属性缓存应用程序上下文locations,因此如果locations第二次出现相同的上下文,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

更新

您的项目是多模块 Maven 项目吗?根据文档:

默认设置是forkCount=1/reuseForks=true,这意味着Surefire创建一个新的JVM进程来执行一个maven模块中的所有测试。