pom中添加Junit依赖,当前最新版本4.12。
junit
junit
4.12
test
Junit官网地址:http://junit.org/junit4/ ,目前普遍使用的是Junit4,下一代Junit5也发布了先导版本,有兴趣可以自行了解。
编写待测试类Caculate.java
package com.loongshawn.task;
public class Caculate {
public int plus(int a,int b){
return a + b;
}
public int minus(int a,int b){
return a - b;
}
}
在Test包下编写测试类:
package com.loongshawn;
import com.loongshawn.task.Caculate;
import org.junit.Test;
public class CaculateTest {
@Test
public void plusTest(){
Caculate caculate = new Caculate();
System.out.println(caculate.plus(3,2));
}
@Test
public void minusTest(){
Caculate caculate = new Caculate();
System.out.println(caculate.minus(3,2));
}
}
进入项目根路径,执行mvn test命令,其结果如下,也能够完成单元测试:
ocalhost:quartz-springboot xiaolong$ mvn test
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.loongshawn.CaculateTest
5
1
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in com.loongshawn.CaculateTest
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.145 s
[INFO] Finished at: 2017-08-04T13:32:36+08:00
[INFO] Final Memory: 15M/226M
[INFO] ------------------------------------------------------------------------
Surefire插件用来在maven构建生命周期的test阶段执行一个应用的单元测试,可以运行Junit写得单元测试。它会产生两种不同形式的测试结果报告:
默认情况下,这些文件生成在工程的${basedir}/target/surefire-reports,目录下(basedir指的是pom文件所在的目录)。
Maven Surefire Plugin与Junit间的搭配关系如下:
if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value
use junit47 provider
if JUnit >= 4.0 is present
use junit4 provider
else
use junit3.8.1
可以看出,Surefire插件看到的JUnit仅三个版本如下,至于具体选用哪个版本,依实际配置而定。
接下来,配置Maven Surefire Plugin,Junit沿用上面的版本。
org.apache.maven.plugins
maven-surefire-plugin
2.20
org.apache.maven.surefire
surefire-junit47
2.20
开始测试,测试内容同上,结果也同上。咱重点关注Surefire的一些常用特性。
默认mvn test命令是全局测试,如果仅需要对其中部分用例测试,则添加一个includes属性,其值为具体java类名,多个以逗好间隔。
org.apache.maven.plugins
maven-surefire-plugin
2.20
PlusTest.java,CaculateTest.java
org.apache.maven.surefire
surefire-junit47
2.20
console输出:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.loongshawn.CaculateTest
5
1
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 s - in com.loongshawn.CaculateTest
[INFO] Running com.loongshawn.PlusTest
9
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.loongshawn.PlusTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.489 s
[INFO] Finished at: 2017-08-04T16:02:28+08:00
[INFO] Final Memory: 23M/216M
[INFO] ------------------------------------------------------------------------
也可以排除部分类不测试则利用excludes属性,详情参考http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
1、Maven Surefire Plugin官网
2、Maven Surefire Plugin属性使用说明