1. 问题描述
项目结构为父子工程结构
— project
----child-common
----child-a
— pom.xml
在上述工程中,执行单元测试时,使用idea中的图形按钮点击的方式启动单元测试,是正常的;但是使用命令mvn test -Dtest=xxxxxxTest#xxxxx是失败的
2. 问题处理
2.1 问题1
[ERROR] Failed to execute goal on project XXXXXX: Could not resolve dependencies for project com.xxxxxxx:xxxxxx:jar:0.0.1-SNAPSHOT: Could not find artifact com.xxxxxx:xxxxx
问题说明:在子工程child-a中使用命令mvn test执行单元测试时,是无法直接找到其所依赖的child-common工程的
问题处理:需要在根目录(父目录)执行mvn clean install -Dmaven.test.skip=true 将父子工程安装到本地,然后再次执行mvn test -Dtest=xxxxxxTest#xxxxx应该就不会报这个错误了
2.2 问题2
java.lang.NullPointerException
问题说明:命令行执行mvn test后,@Autowired注入的bean为null导致空指针异常。查阅相关资料后,了解到maven-surefire-plugin:2.12.4是过时的,可能不支持junit5 + spring5了
问题处理:
升级maven-surefire-plugin版本到2.22.0,即在child-a的pom.xml中加入如下依赖:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
然后再在子工程child-a目录下执行mvn test -Dtest=xxxxxxTest#xxxxx应该就可以了
参考:https://mkyong.com/maven/maven-test-failed-on-spring-autowired-and-junit-5/