我已将 Cargo 设置为在 Maven 配置文件中的预集成测试阶段启动 glassfish 实例。然后,我的测试在集成测试阶段运行,最后,Cargo 在集成测试后阶段关闭 tomcat 实例。
当所有测试通过时,这非常有用,但如果任何测试失败,maven 构建就会失败,并且似乎永远不会达到集成后测试阶段,这会使 glassfish 实例继续运行(而我无法停止它)而不终止进程)。
我做错了什么吗?有没有办法确保即使集成测试阶段失败,cargo 也会关闭我的 glassfish 实例?
我的 Maven 简介:
<profile>
<!-- run integration tests against the app deployed to a container -->
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- override the exclusion and include integration tests -->
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>***IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo.plugin.version}</version>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<containerId>glassfish3x</containerId>
<artifactInstaller>
<groupId>org.glassfish.main.distributions</groupId>
<artifactId>glassfish</artifactId>
<version>${glassfish.version}</version>
</artifactInstaller>
</container>
<configuration>
<properties>
<cargo.datasource.datasource.mysql>
cargo.datasource.jndi=jdbc/TrackerPool|
cargo.datasource.driver=com.mysql.jdbc.Driver|
cargo.datasource.url=jdbc:mysql://localhost/[database]|
cargo.datasource.transactionsupport=LOCAL_TRANSACTION|
cargo.datasource.username=[username]|
cargo.datasource.password=[password]
</cargo.datasource.datasource.mysql>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</profile>
最佳答案
问题只是基于 maven-surefire-plugin 的错误使用,该插件旨在与单元测试相关,但不用于集成测试。为此目的 maven-failsave-plugin存在这将解决您的问题。
使用 maven-failsave-plugin 可以让您免于定义 include rule for integration tests 。 Maven 中集成测试的通常命名约定是这样的:
IT*.java
*IT.java
*ITCase.java
因此,我建议相应地命名您的集成测试,这样您就不需要任何类型的排除/包含规则,无论是 maven-surefire-plugin (单元测试)还是 maven-failsafe-plugin (集成测试)。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
仅当您希望在集成测试失败的情况下使构建失败时,才需要验证目标。你必须像这样调用maven:
mvn -Pprofile clean verify
关于java - 当集成测试失败时,maven 不会停止通过 Cargo 的 Glassfish 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14294545/