当前位置:网站首页>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
边栏推荐
- Introduction to spring trick of ByteDance: senior students, senior students, senior students, and the author "brocade bag"
- Wu Jun's trilogy experience (VII) the essence of Commerce
- Program counter of JVM runtime data area
- JVM之垃圾回收器上篇
- JVM class loading subsystem
- 数据仓库建模使用的模型以及分层介绍
- Idea resolving jar package conflicts
- EasyRE WriteUp
- Koa Middleware
- Flink 解析(二):反压机制解析
猜你喜欢
随机推荐
Jetpack compose 1.1 release, based on kotlin's Android UI Toolkit
Assembly language addressing mode
案例:检查空字段【注解+反射+自定义异常】
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items
Akamai浅谈风控原理与解决方案
JVM 垃圾回收器之Serial SerialOld ParNew
Akamai 反混淆篇
MySQL日期函数
逻辑运算指令
Flink 解析(四):恢复机制
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
Only learning C can live up to expectations TOP4 S1E6: data type
控制转移指令
信息与网络安全期末复习(基于老师给的重点)
Activiti directory (III) deployment process and initiation process
js垃圾回收机制和内存泄漏
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之垃圾回收器下篇
Interpretation of Flink source code (III): Interpretation of executiongraph source code