当前位置:网站首页>Junit单元测试
Junit单元测试
2022-07-06 09:33:00 【bestkasscn】
Junit单元测试
简介
JUnit 是一个 Java 编程语言的单元测试框架。JUnit 在测试驱动的开发方面有很重要的发展,是起源于 JUnit 的一个统称为 xUnit 的单元测试框架之一。
Junit是一个自动化测试框架,可以帮助开发人员对接口和方法进行单元测试,只需查看最后结果就知道整个项目的接口或代码是否流畅
导入方法
- 使用maven导入
在maven的<dependencies></dependencies>里添加如下代码
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- 使用官方的jar包
使用方法
首先我们在src.main.java目录创建一个Calculator类
public class Calculator {
public int add(int a,int b){
return a + b;
}
public int sub(int a,int b){
return a - b;
}
public int div(int a,int b){
return a / b;
}
public int mul(int a,int b){
return a * b;
}
}
再在src.main.java目录创建一个CalculatorTest类,我们这里以sub方法为例测试sub方法
import com.bestkasscn.CatCatProject.Calculator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
public void testDiv(){
Calculator calculator = new Calculator();
int result = calculator.sub(1, 2);
Assertions.assertEquals(-1,result);
}
}
在类中我们定义了一个testDiv()方法,该方法即为测试方法,在方法的前面我们需要加上@Test注解,代表这是一个测试方法,并且方法要求空参,不含返回值,方法名任取,但尽量与你要测试的方法有关
判定结果:
* 红色:失败
* 绿色:成功
* 一般我们会使用断言操作来处理结果
* Assertions.assertEquals(期望的结果,运算的结果);

写好测试方法后,点击方法左侧的绿色按钮就可以开始测试了
测试结果如下
绿色就代表通过了。
总结
* 步骤:
1. 定义一个测试类(测试用例)
* 建议:
* 测试类名:被测试的类名Test CalculatorTest
2. 定义测试方法:可以独立运行
* 建议:
* 方法名:test测试的方法名 testDiv()
* 返回值:void
* 参数列表:空参
3. 给方法加@Test
4. 导入junit依赖环境(maven或jar包)
* 判定结果:
* 红色:失败
* 绿色:成功
* 一般我们会使用断言操作来处理结果
* Assertions.assertEquals(期望的结果,运算的结果);
* 补充:
* @Before:
* 修饰的方法会在测试方法之前被自动执行
* @After:
* 修饰的方法会在测试方法执行之后自动被执行
边栏推荐
- Garbage first of JVM garbage collector
- Resume of a microservice architecture teacher with 10 years of work experience
- koa中间件
- ByteDance technical Interviewer: what kind of candidate do I want to pick most
- Install docker under windows10 (through Oracle VM VirtualBox)
- Flink 解析(七):时间窗口
- JVM垃圾回收概述
- mysql的合计/统计函数
- Control transfer instruction
- vscode
猜你喜欢

Activiti directory (IV) inquiry agency / done, approved

8086 CPU 内部结构

Akamai 反混淆篇

Set up the flutter environment pit collection

Resume of a microservice architecture teacher with 10 years of work experience

The daemon thread starts redis and modifies the configuration file

Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here

逻辑运算指令

ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items

Akamai浅谈风控原理与解决方案
随机推荐
关于Stream和Map的巧用
CentOS7上Redis安装
Activiti directory (III) deployment process and initiation process
DS18B20數字溫度計系統設計
Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
Use of mongodb in node
Only learning C can live up to expectations TOP4 S1E6: data type
唯有学C不负众望 TOP3 Demo练习
Prototype chain inheritance
8086 CPU internal structure
Only learning C can live up to expectations top3 demo exercise
Ce n'est qu'en apprenant que c est à la hauteur des attentes Top5 s1e8 | s1e9: caractères et chaînes & opérateurs arithmétiques
一个数10年工作经验的微服务架构老师的简历
MySQL date function
Interpretation of Flink source code (III): Interpretation of executiongraph source code
PostgreSQL 14.2, 13.6, 12.10, 11.15 and 10.20 releases
After the subscript is used to assign a value to the string type, the cout output variable is empty.
JVM garbage collector part 1
Only learning C can live up to expectations Top1 environment configuration
案例:检查空字段【注解+反射+自定义异常】