这是我们单元测试的基类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring/test-context.xml")
public abstract class BaseUnitTest {}
所有单元测试都扩展了这个类。
当我在 Eclipse 中本地运行测试时(使用 Run As > Unit Test),测试运行大约 5 秒,因为所有测试共享相同的上下文并且它只加载一次。
但是,当我使用 mvn 测试目标运行它们时,大约需要 5 分钟。查看日志后,我看到每次测试都在加载应用程序上下文。我们在 Jenkins CI 服务器上运行它需要同样的时间(5 分钟)。
不知道发生了什么。在 spring 文档中,它声明即使使用 maven 也应该重用 appContext,但这里不是这种情况。
任何帮助,将不胜感激。
更新: 我在打开调试标志的情况下运行 mvn,我看到每个测试都生成了一个新的 JVM:
分叉命令行: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefirebooter8169952914558366417.jar S:\git\picaxo21\picaxo\picaxoService\target\ surefire\surefire8550033206398936560tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_05655453605766528120tmp"
分叉命令行: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefirebooter4002024477779069323.jar S:\git\picaxo21\picaxo\picaxoService\target\ surefire\surefire6735432532690834115tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_17783676008756503456tmp"
分叉命令行: cmd.exe /X /C "java -Xverify:none -jar S:\git\picaxo21\picaxo\picaxoService\target\surefire\ 7874269889863176184.jar S:\git\picaxo21\picaxo\picaxoService\target\ surefire\surefire2050758518148174678tmp S:\git\picaxo21\picaxo\picaxoService\target\surefire\surefire_27591156970671336255tmp"
我正在使用 forkCount=1 和 reuseForks=true 所以我不确定为什么会这样。任何线索?
父 POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
.....
</parent>
<groupId>...</groupId>
<artifactId>picaxo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>.....</name>
<description>Build All Modules</description>
<modules>
<module>picaxoService</module>
</modules>
<scm>
....
</scm>
<properties>
<pmd.include.tests>true</pmd.include.tests>
<findbugs.plugin.version>3.0.0</findbugs.plugin.version>
<fb.threshold>Low</fb.threshold>
<fb.includeTests>true</fb.includeTests>
<fb.effort>Max</fb.effort>
<fb.failOnError>false</fb.failOnError>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>true</skipTests>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>${skipTests}</skipTests>
<reuseForks>true</reuseForks>
<forkCount>1</forkCount>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</build>