当前位置:网站首页>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:
* 修饰的方法会在测试方法执行之后自动被执行
边栏推荐
- Activiti directory (III) deployment process and initiation process
- JVM 垃圾回收器之Garbage First
- Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.
- GCC error: terminate called after throwing an instance of 'std:: regex_ error‘ what(): regex
- 唯有学C不负众望 TOP1环境配置
- 8086 memory
- 唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
- Typescript basic operations
- DS18B20數字溫度計系統設計
- Eight part essay that everyone likes
猜你喜欢

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

Activiti目录(五)驳回、重新发起、取消流程

MySQL日期函数

1. Introduction to JVM

Wu Jun's trilogy experience (VII) the essence of Commerce

MySQL字符串函数

Description of project structure configuration of idea
![[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.](/img/ef/2072aac5f85c7daf39174784dec7ee.jpg)
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.

Activiti目录(三)部署流程、发起流程

TCP's three handshakes and four waves
随机推荐
ByteDance technical Interviewer: what kind of candidate do I want to pick most
汇编课后作业
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items
À propos de l'utilisation intelligente du flux et de la carte
SQL tuning notes
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
Flink 解析(二):反压机制解析
MySQL string function
Flink 解析(七):时间窗口
Flink 解析(四):恢复机制
JVM之垃圾回收器上篇
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
JVM之垃圾回收器下篇
汇编语言基础知识
JVM garbage collector part 2
Data transfer instruction
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.