Maven多模块项目集成测试类加载但内部测试不执行问题
问题:Maven Failsafe加载集成测试类但不执行嵌套测试方法
我已查阅StackOverflow相关问题,但未能解决问题。在Eclipse运行配置中执行mvn clean install时,UserGetRolesIT、UserSetRolesIT等集成测试类可被加载,但类内的嵌套测试方法完全未执行。
项目配置信息
项目目录结构
AuthorizationMS (项目根目录) |-- AuthorzationMS |-- pom.xml (含集成测试的项目A) |-- AMS-Client |-- pom.xml (含集成测试的项目B) |-- pom.xml (父pom.xml)
父pom.xml内容
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx.xxx.ams</groupId> <artifactId>AuthorizationMSParent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>AuthorizationMSParent</name> <modules> <module>AMS-Client</module> <module>AuthorizationMS</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> ..... </repositories> <dependencies> .... </dependencies> </project>
项目A的pom.xml内容
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx.xxx.authms</groupId> <artifactId>AuthorizationMS</artifactId> <version>1.0</version> <packaging>war</packaging> <name>AuthorizationMS</name> <parent> <artifactId>AuthorizationMSParent</artifactId> <groupId>com.xxx.xxx.ams</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> .... </repositories> <pluginRepositories> <pluginRepository> <id>repo</id> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <dependencies> ... </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.19.1</version> <configuration> <includes> <include>**/*IT</include> </includes> </configuration> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> <configuration> <excludes> <exclude>none</exclude> </excludes> <includes> <include>**/*IT</include> </includes> </configuration> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> <configuration> <warName>AuthorizationMS</warName> </configuration> </plugin> </plugins> </build> </project>
执行mvn clean install的日志输出
------------------------------------------------------- T E S T S ------------------------------------------------------- Jan 17, 2024 4:59:09 PM com.xxx.xxx.authms.data.service.CloudantServiceManager init INFO: CloudantServiceManager | init method start Jan 17, 2024 4:59:10 PM com.xxx.xxx.authms.data.service.CloudantServiceManager init INFO: CloudantServiceManager | init total: 762 Running com.xxx.xxx.authms.test.UserGetRolesIT Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.xxx.xxx.authms.test.UserGetRolesIT Running com.xxx.xxx.authms.test.UserSetRolesIT Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.xxx.xxx.authms.test.UserSetRolesIT
集成测试类代码示例
public class UserGetRolesIT { private static final Gson gson = new Gson(); private static AuthorizationDataService authDataService = AuthorizationDataService.getInstance(); @Nested @DisplayName("Test Getting Roles") class TestGettingUserRolesIT { @ParameterizedTest @DisplayName("getRoles valid users") @ValueSource(strings = { "user@xxx.com", "Some Name/Place/Compnay", "David Luck/Somewhere/Company", "Newton Apple/Place/Company", "user2@xxx.com", "" }) void isUserFoundIT(String userId) throws AuthException { assertTrue(authDataService.getRole(userId).getRole() != null, userId + " is not being found"); } @Test @DisplayName("getRoles RoleByIds") void getRolesByIds() throws IOException, URISyntaxException, AuthException { URL testFileURL = UserGetRolesIT.class.getResource("/roles/GetRoles_Role_By_Ids.json"); String content = Files.readString(Paths.get(testFileURL.toURI())); // Turn our test .json file into a real object AuthorizationData info = gson.fromJson(content, AuthorizationData.class); Map<String, Role> rolesMap = authDataService.getRolesByDeliverableIds(info.getUserId(), info.getDeliverableIds()); assertTrue(!rolesMap.isEmpty(), ""); assertTrue(rolesMap.containsValue(Role.READER), "Response came back false!"); } }
问题根源与解决方案
问题根源
你使用的maven-failsafe-plugin版本是2.19.1,该版本仅支持JUnit 4,而测试类中使用了JUnit 5特有的@Nested注解,低版本插件无法识别该特性,导致嵌套类中的测试方法无法被执行。
解决方案
- 升级maven-failsafe-plugin版本:将版本升级到3.0.0及以上(与你使用的maven-surefire-plugin版本保持一致最佳),该版本开始全面支持JUnit 5特性。
- 确保JUnit 5依赖完整:确认项目中已引入JUnit 5的核心依赖,示例配置如下:
<dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> </dependencies>
- 简化Failsafe插件配置:升级后的插件无需额外配置即可识别JUnit 5测试,简化后的配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
完成以上调整后,重新执行mvn clean install,嵌套类中的测试方法即可正常执行。
内容的提问来源于stack exchange,提问作者Doug
相关产品推荐
相关产品推荐

