之前写单元测试一般都是选中方法后点击执行,今天使用mvn test执行的时候却一直不执行,其中遇到了两大坑,记录一下。项目是springboot项目.
先说运行了mvn test不执行的问题:现象是控制台输出了下面的信息后就不再输出了
[INFO] Scanning for projects... Downloading: http://repo.maven.apache.org/maven2/com/fasterxml/jackson/jackson-bom/2.8.10/jackson-bom-2.8.10.pom Downloading: http://repo.maven.apache.org/maven2/org/apache/logging/log4j/log4j-bom/2.7/log4j-bom-2.7.pom Downloading: http://repo.maven.apache.org/maven2/org/springframework/spring-framework-bom/4.3.13.RELEASE/spring-framework-bom-4.3.13.RELEASE.pom Downloading: http://repo.maven.apache.org/maven2/org/springframework/data/spring-data-releasetrain/Ingalls-SR9/spring-data-releasetrain-Ingalls-SR9.pom Downloading: http://repo.maven.apache.org/maven2/org/springframework/integration/spring-integration-bom/4.3.12.RELEASE/spring-integration-bom-4.3.12.RELEASE.pom Downloading: http://repo.maven.apache.org/maven2/org/springframework/security/spring-security-bom/4.2.3.RELEASE/spring-security-bom-4.2.3.RELEASE.pom
猜测是 http://repo.maven.apache.org... ,在网上查了下,说是需要在maven的setting文件里加镜像,参考的是这篇blog: setting里配置镜像
加的配置如下:
<mirrors> <mirror> <id>repo</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>https://repo.maven.apache.org/maven2/</url> </mirror> </mirrors>
加好镜像再次执行的时候,所依赖的Jar包都可以顺利的下载下来了。
但是单元测试执行的结果都是0:
------------------------------------------------------- T E S T S ------------------------------------------------------- Running TestSuite Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.914 sec Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
将maven-surefire-plugin这个插件从2.6升级到2.19.1版本后,单元测试就可以正常执行了
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <!--2.6的版本会导致mvn test命令执行之后Tests run: 0,也就是 不会执行任务测试用例,得使用高版本--> <!--<version>2.6</version>--> <configuration> <skip>false</skip> </configuration> </plugin>