当前位置:网站首页>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:
* 修饰的方法会在测试方法执行之后自动被执行
边栏推荐
- Instructions for Redux
- List集合数据移除(List.subList.clear)
- Flink源码解读(二):JobGraph源码解读
- Log4j2 major vulnerabilities and Solutions
- JVM garbage collection overview
- Program counter of JVM runtime data area
- 在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
- 肖申克的救赎有感
- Interpretation of Flink source code (III): Interpretation of executiongraph source code
- In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
猜你喜欢
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
JVM garbage collector part 1
Some instructions on whether to call destructor when QT window closes and application stops
Wu Jun's trilogy insight (V) refusing fake workers
Shawshank's sense of redemption
原型链继承
Koa Middleware
Description of project structure configuration of idea
IDEA断点调试技巧,多张动图包教包会。
Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
随机推荐
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
Interpretation of Flink source code (III): Interpretation of executiongraph source code
汇编语言基础知识
Mongodb learning notes
面试集锦库
List集合数据移除(List.subList.clear)
唯有学C不负众望 TOP3 Demo练习
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
Basic knowledge of assembly language
mysql的列的数据类型详解
JVM garbage collector part 2
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
Some feelings of brushing leetcode 300+ questions
Only learning C can live up to expectations Top1 environment configuration
Interview collection library
吴军三部曲见识(四) 大家智慧
吴军三部曲见识(五) 拒绝伪工作者
Prototype chain inheritance
Activiti directory (V) reject, restart and cancel process
Use of mongodb in node