一、概念

junit是一个专门测试的框架

集合maven进行单元测试,可批量测试类中的大量方法是否符合预期

二、作用:
单元测试:测试的内容是类中的方法,每一个方法都是独立测试的。方法是测试的基本单位。

三、使用方法
1、pom内加入依赖

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

2、在maven中src/test/java目录下的,创建测试程序。
推荐的创建类和方法的提示:
1、测试类的名称:Test+待测试类名
2、测试方法的名称:Test+方法名称
例如:你要测试HelloMaven
创建测试类TestHelloMaven
@Test
public void testaDD(){
测试HelloMaven的add方法是否正确
}
其中testAdd叫做测试方法,定义规则:
1、方法必须是public的
2、方法必须没有返回值
3、方法名称自定义,推荐是Test+被测方法
4、方法上面加上注解@Test

四、举例,Hello项目

1、新建java源程序,存放在Hello\src\main\java\com\testbk目录下,取名HelloMaven.java

package com.testbk;
import org.junit.Assert;
import org.junit.Test; public class TestHelloMaven{
@Test
public void testAdd(){
System.out.println("maven junit testAdd()===")
HelloMaven hello = new HelloMaven();
int res = hello.add(10,20);
//验证10+20是不是30,juit提供的方法,对比结果的
//assertEquals(期望值,实际值)
Assert.assertEquals(30,res)
}
}

2、新建maven测试类型,存放在Hello\src\main\java\com\testbk目录下,取名TestHelloMaven.java

package com.testbk;
import org.junit.Assert;
import org.junit.Test; public class TestHelloMaven{
@Test
public void testAdd(){
System.out.println("maven junit testAdd()===")
HelloMaven hello = new HelloMaven();
int res = hello.add(10,20);
//验证10+20是不是30,juit提供的方法,对比结果的
//assertEquals(期望值,实际值)
Assert.assertEquals(30,res)
}
@Test
public void testAdd2(){
System.out.println("#####maven junit testAdd()2###");
HelloMaven hello = new HelloMaven();
int res = hello.add(10,20);
//验证10+20是不是30,juit提供的方法,对比结果的
//assertEquals(期望值,实际值)
Assert.assertEquals(50,res);
}
}

3、执行mvn clean:清理target目录

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.testbk:testjava >-------------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testjava ---
[INFO] Deleting D:\javaProjects\Hello\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.196 s
[INFO] Finished at: 2021-04-25T22:50:10+08:00
[INFO] ------------------------------------------------------------------------

4、执行mvn compile:编译main/java目录下的java为class文件,同时把class拷贝到target/classes目录下面

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.testbk:testjava >-------------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\javaProjects\Hello\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.724 s
[INFO] Finished at: 2021-04-25T22:54:23+08:00
[INFO] ------------------------------------------------------------------------

5、执行mvn test-compile:编译test/java目录下的java为class文件,同时class拷贝到target/test-classes目录下面

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.testbk:testjava >-------------------------
[INFO] Building maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testjava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testjava ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\javaProjects\Hello\target\test-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.764 s
[INFO] Finished at: 2021-04-25T22:55:43+08:00
[INFO] ------------------------------------------------------------------------

6、执行mvn test:查看测试结果,通过1,失败1,并在指定目录生成测试报告

Results :

Failed tests:   testAdd2(com.testbk.TestHelloMaven): expected:<50> but was:<30>

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.050 s
[INFO] Finished at: 2021-04-25T22:57:16+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project testjava: There are test failures.
[ERROR]
[ERROR] Please refer to D:\javaProjects\Hello\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

7、修改测试代码,并再次执行mvn test

package com.testbk;
import org.junit.Assert;
import org.junit.Test; public class TestHelloMaven{
@Test
public void testAdd(){
System.out.println("=====maven junit testAdd()===");
HelloMaven hello = new HelloMaven();
int res = hello.add(10,20);
//验证10+20是不是30,juit提供的方法,对比结果的
//assertEquals(期望值,实际值)
Assert.assertEquals(30,res);
}
@Test
public void testAdd2(){
System.out.println("#####maven junit testAdd()2###");
HelloMaven hello = new HelloMaven();
int res = hello.add(30,20);
//验证10+20是不是30,juit提供的方法,对比结果的
//assertEquals(期望值,实际值)
Assert.assertEquals(50,res);
}
}

查看运行结果,测试通过

 T E S T S
-------------------------------------------------------
Running com.testbk.TestHelloMaven
=====maven junit testAdd()===
#####maven junit testAdd()2###
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.282 s
[INFO] Finished at: 2021-04-25T22:58:41+08:00
[INFO] ------------------------------------------------------------------------

最新文章

  1. HTTPS 互联网世界的安全基础
  2. 软件工程:黄金G点小游戏1.0
  3. 71 mac boook pro 无 gpu 下caffe 安装
  4. 如何防止ElasticSearch集群出现脑裂现象(转)
  5. 2014 New Year&rsquo;s First Blog
  6. A Full Hardware Guide to Deep Learning
  7. skynet是什么
  8. MAC itunes无法验证服务器s.mzstatic/itunes无法更新服务器解决方案
  9. django: template - built-in tags
  10. SQL 通配符
  11. jQuery的主要用法
  12. Linux-2.6.32内核编译流量计数器nfacct
  13. JSP页面中的pageEncoding和contentType的区别
  14. 电影《Green book》观后感_已补全:携带着种族歧视的“光环”,艰难地获得朋友的相互依赖,依然得享受生活的酸甜苦咸。
  15. Luogu 3690 LCT - 模板
  16. BZOJ 4720 [Noip2016]换教室
  17. 完美.PCK文件不完全详解
  18. [转]Bootstrap table后端分页(ssm版)
  19. 41.oracle索引,分析索引,索引碎片整理
  20. 使用windows live writer发表的博客

热门文章

  1. Jmeter的脚本参数化
  2. APP跳转小程序,小程序跳转APP
  3. C++指针的算术运算 、关系运算
  4. Nginx常用内核参数优化,安装,基本命令
  5. [实战]ASP.NET Padding Oracle信息泄露漏洞
  6. slickgrid ( nsunleo-slickgrid ) 4 解决区域选择和列选择冲突
  7. SpringMVC-06 Ajax
  8. Python字典与集合
  9. beego 框架用的页面样式模板
  10. 2019 GDUT Rating Contest I : Problem E. Convention