我有一个 Spring Boot 多模块项目,它满足除集成测试之外的所有要求。作为测试阶段的一部分,在src/test中定义的单元测试在 surefire 中可以正常工作。
多个小时的故障保护试验并没有带来预期的结果。故障安全无法找到任何测试。我假设这是基于:
- 测试类不是与每个模块关联的 .jar 文件的一部分。
- 我无法弄清楚如何在故障安全将查找测试的类路径上获取测试 .jar 文件。
我的项目:
project folder
--pom.xml
|
----application
------pom.xml
------src
--------main
--------test
|
----model
------pom.xml
|
------data
--------pom.xml
--------src
----------main
----------test
|
------repository
--------pom.xml
--------src
----------main
----------test
我的父母pom:
<project>
....
<artifactId>master</artifactId>
....
<modules>
<module>model</module>
<module>application</module>
</modules>
....
<dependencyManagement>
<dependencies>
<!-- Model -->
<dependency>
<groupId>xxx.xxx.xxx</groupId>
<artifactId>data</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>xxx.xxx.xxx</groupId>
<artifactId>repository</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Application -->
<dependency>
<groupId>xxx.xxx.xxx</groupId>
<artifactId>application</artifactId>
<version>${project.version}</version>
</dependency>
</dependencyManagement>
....
<build>
<!-- defined here and then used on a module-by-module basis -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Configure failsafe -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
<additionalClasspathElements>
<additionalClasspathElement>
${project.basedir}/../../model/data/target/data-${project.version}-tests.jar
</additionalClasspathElement>
</additionalClasspathElements>
<dependenciesToScan>
<dependency>
com.optum.cirrus:repository
</dependency>
</dependenciesToScan>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugin>
</plugins>
</pluginManagement>
....
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
请注意,我为所有模块创建了一个 test-jar 以及一个基于主代码的 jar。这仅适用于 test-jar 文件中的src/test代码。
模块 pom 文件是:
<project>
<parent>
<artifactId>master</artifactId>
</parent>
....
<artifactId>model</artifactId>
<packaging>pom</packaging>
<modules>
<module>data</module>
<module>repository</module>
</modules>
....
</project>
<project>
....
<parent>
<artifactId>model</artifactId>
</parent>
<artifactId>data</artifactId>
<packaging>jar</packaging>
<name>data</name>
....
</project>
<project>
....
<parent>
<artifactId>model</artifactId>
</parent>
<artifactId>repository</artifactId>
<packaging>jar</packaging>
<name>repository</name>
<dependencies>
<dependency>
<groupId>xxx.xxx.xxxx</groupId>
<artifactId>data</artifactId>
</dependency>
</dependencies>
....
</project>
<project>
....
<parent>
<artifactId>model</artifactId>
</parent>
<artifactId>master</artifactId>
<packaging>jar</packaging>
<name>application</name>
<dependencies>
<dependency>
<groupId>xxx.xxx.xxxx</groupId>
<artifactId>resources</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
故障保护仅作为应用程序构建的一部分运行。这是在处理完 Spring Boot 重新打包之后,尽管原始和重新打包的 jar 文件都可用。
当我使用调试运行 maven 时,我看到重新打包的应用程序 jar 加上资源 jar [我假设这是基于故障安全上定义的依赖项元素] 加上 resources-test.jar [假设基于附加类路径元素元素存在] 在类路径上。
我不知道如何配置故障安全,以便它可以找到包含在 test.jar 文件中的测试类,就像在仅涉及肯定火和单个模块时找到单元测试的方式一样。
一种未开发的可能性可能是从例如资源 pom.xml 创建第二个工件,该工件将创建 test.jar,然后我可以将其作为依赖项传递给故障安全。请注意,“只需创建一个仅包含src/test且依赖于src/main代码的单独模块”可能会实现该结果,但该解决方案似乎是一个真正的混搭......适合 Maven 模型的文件结构。
生活不能那么离奇,正在做的事情也不能如此“一次性”,以至于尚未达到预期的结果。这意味着我显然做错了什么,但不知道是什么。任何建议,将不胜感激。
谢谢。