如何通过Maven在MacOS命令行运行集成/组件测试
问题解决:Maven集成测试未执行 + JAXBException报错
一、Maven命令未执行测试的修复方案
1. 确认测试类命名规则
Maven Surefire插件(mvn test默认使用)只会自动执行符合以下命名的测试类:
**/Test*.java*/*Test.java*/*Tests.java*/*TestCase.java
如果你的集成测试类用*IT.java这类行业通用命名,需要改用Maven Failsafe插件,执行mvn verify目标而非test目标,同时在pom.xml中配置Failsafe插件:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.0.0-M7</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. 修正Spring配置文件路径
你指定的classpath:/src/test/resources/application-component-test.properties路径错误,src/test/resources本身就是测试环境的classpath根目录,无需额外指定路径。直接激活profile即可,Spring会自动加载对应配置文件:
mvn verify -Dspring.profiles.active=component-test -Dsurefire.failIfNoSpecifiedTests=false
(如果是单元测试,替换为mvn test)
3. 明确指定测试类
如果没有符合命名规则的测试类,可直接在命令中指定要执行的类:
mvn test -Dtest=YourComponentTestClass -Dspring.profiles.active=component-test
二、解决NoClassDefFoundError: jakarta/xml/bind/JAXBException
Java 9及以上版本移除了JAXB作为JDK默认模块,需手动引入依赖:
1. 直接引入JAXB依赖
在pom.xml中添加:
<dependencies> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>3.0.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>3.0.1</version> <scope>test</scope> <optional>true</optional> </dependency> </dependencies>
2. Spring Boot项目简化方案
如果是Spring Boot项目,可直接引入包含JAXB的starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <scope>test</scope> </dependency>
3. IntelliJ缓存修复
IntelliJ出现该报错时,尝试清除缓存:
- 点击
File->Invalidate Caches... - 选择
Invalidate and Restart
内容的提问来源于stack exchange,提问作者Alexandra Dediu
相关产品推荐
相关产品推荐

