Maven工程使用Junit进行单元测试
2017-08-04 11:14
423 查看
1.仅利用Junit测试
pom中添加Junit依赖,当前最新版本4.12。<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
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] ------------------------------------------------------------------------
2.利用Maven Surefire Plugin+Junit测试
Surefire插件用来在maven构建生命周期的test阶段执行一个应用的单元测试,可以运行Junit写得单元测试。它会产生两种不同形式的测试结果报告:1).纯文本
2).xml文件格式的
默认情况下,这些文件生成在工程的${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仅三个版本如下,至于具体选用哪个版本,依实际配置而定。
JUnit 3.8.x
JUnit 4.x (surefire提供串行执行的provider)
JUnit 4.7 (surefire提供支持并行执行的provider)
接下来,配置Maven Surefire Plugin,Junit沿用上面的版本。
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.20</version> </dependency> </dependencies> </plugin> </plugins>
开始测试,测试内容同上,结果也同上。咱重点关注Surefire的一些常用特性。
2.1仅测试指定类
默认mvn test命令是全局测试,如果仅需要对其中部分用例测试,则添加一个includes属性,其值为具体java类名,多个以逗好间隔。<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <includes> <include>PlusTest.java,CaculateTest.java</include> </includes> </configuration> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.20</version> </dependency> </dependencies> </plugin>
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
3.参考
1、Maven Surefire Plugin官网2、Maven Surefire Plugin属性使用说明
相关文章推荐
- maven下使用junit对spring进行单元测试_01基本应用
- 使用 JUnit 进行 Java 代码的单元测试
- 使用junit进行单元测试
- 使用Junit4对由Maven管理的Spring项目进行单元测试
- 使用Junit进行单元测试
- 使用Junit和hamcrest包进行单元测试
- Junit(1)在Eclipse中使用JUnit4进行单元测试
- 使用JUnit在struts+spring+hibernate框架环境下进行单元测试
- Unrooted Tests:使用jUnit进行单元测试时遇到的问题
- 使用Junit对Spring进行单元测试实战小结
- Eclipse使用jUnit4对MapReduce程序进行单元测试
- 单元测试第三弹——使用JUnit进行单元测试
- Eclipse学习4-在Eclipse中使用JUnit进行单元测试(上)
- 使用Junit等工具进行单元测试
- maven下使用 junit对 spring进行单元测试
- 使用junit4+NekoHTML对页面进行单元测试
- Maven项目中使用JUnit进行单元测试
- 使用JUnit在struts+spring+hibernate框架环境下进行单元测试
- 使用Junit进行单元测试代码
- eclipse上使用JUnit框架进行单元测试