当前位置:网站首页>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
边栏推荐
猜你喜欢
Log4j2 major vulnerabilities and Solutions
Akamai浅谈风控原理与解决方案
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
Shawshank's sense of redemption
Koa Middleware
8086 CPU internal structure
信息与网络安全期末复习(基于老师给的重点)
JVM之垃圾回收器上篇
Set up the flutter environment pit collection
汇编语言寻址方式
随机推荐
1. JVM入门介绍
Only learning C can live up to expectations TOP4 S1E6: data type
Only learning C can live up to expectations top2 P1 variable
Only learning C can live up to expectations Top1 environment configuration
Flink 解析(二):反压机制解析
数据仓库建模使用的模型以及分层介绍
JVM之垃圾回收器下篇
Shawshank's sense of redemption
Brush questions during summer vacation, ouch ouch
面试集锦库
Install docker under windows10 (through Oracle VM VirtualBox)
Interpretation of Flink source code (III): Interpretation of executiongraph source code
Prototype chain inheritance
JVM运行时数据区之程序计数器
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
MySQL字符串函数
vscode
Login to verify the simple use of KOA passport Middleware
8086 CPU 内部结构
连接局域网MySql