当前位置:网站首页>JUnit unit test
JUnit unit test
2022-07-06 17:20:00 【bestkasscn】
Junit unit testing
brief introduction
JUnit It's a Java Unit testing framework for programming languages .JUnit There are important developments in Test Driven Development , It originated from JUnit One of them is called xUnit One of the unit testing frameworks of .
Junit It's an automated testing framework , It can help developers unit test interfaces and methods , Just check the final result to know whether the interface or code of the whole project is smooth
The import method
- Use maven Import
stay maven Of <dependencies></dependencies> Add the following code
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- Use official jar package
Usage method
First of all we have src.main.java Create a directory Calculator class
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;
}
}
And then src.main.java Create a directory CalculatorTest class , We are here to sub Method as an example sub Method
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);
}
}
In the class, we define a testDiv() Method , This method is the test method , We need to add @Test annotation , It means this is a test method , And the method requires null parameters , No return value , Method name optional , But try to be related to the method you want to test
The verdict :
* Red : Failure
* green : success
* Generally, we use assertion operations to process the results
* Assertions.assertEquals( Expected results , Result of operation );

After writing the test method , Click the green button on the left side of the method to start the test
The test results are as follows 
Green means passing .
summary
* step :
1. Define a test class ( The test case )
* Suggest :
* Test class name : The name of the class being tested Test CalculatorTest
2. Define test methods : Can run independently
* Suggest :
* Method name :test Test method name testDiv()
* Return value :void
* parameter list : Empty ginseng
3. Add to the method @Test
4. Import junit Depend on the environment (maven or jar package )
* The verdict :
* Red : Failure
* green : success
* Generally, we use assertion operations to process the results
* Assertions.assertEquals( Expected results , Result of operation );
* Add :
* @Before:
* Modified methods are automatically executed before testing methods
* @After:
* The modified method is automatically executed after the test method is executed
边栏推荐
- Activiti directory (I) highlights
- On the clever use of stream and map
- Shawshank's sense of redemption
- 唯有学C不负众望 TOP2 p1变量
- February database ranking: how long can Oracle remain the first?
- CentOS7上Redis安装
- 關於Stream和Map的巧用
- Control transfer instruction
- Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
- Flink parsing (VI): savepoints
猜你喜欢

Flink 解析(二):反压机制解析

Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.

1. JVM入门介绍

"One year after graduation, I won ACL best paper"

IDEA断点调试技巧,多张动图包教包会。

数据仓库建模使用的模型以及分层介绍

Install docker under windows10 (through Oracle VM VirtualBox)

Program counter of JVM runtime data area

Mongodb learning notes

Instructions for Redux
随机推荐
Log4j2 major vulnerabilities and Solutions
JS garbage collection mechanism and memory leakage
搭建flutter环境入坑集合
SQL调优小记
Von Neumann architecture
信息与网络安全期末复习(基于老师给的重点)
【逆向】脱壳后修复IAT并关闭ASLR
koa中间件
8086 CPU internal structure
汇编语言基础知识
mysql 基本增删改查SQL语句
Flink 解析(一):基础概念解析
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
MySQL date function
Description of project structure configuration of idea
TCP's three handshakes and four waves
Compile homework after class
JVM class loading subsystem
Flink源码解读(二):JobGraph源码解读
吴军三部曲见识(七) 商业的本质