当前位置:网站首页>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
边栏推荐
- 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
- Interpretation of Flink source code (III): Interpretation of executiongraph source code
- 【逆向】脱壳后修复IAT并关闭ASLR
- 8086 分段技术
- MySQL字符串函数
- Jetpack compose 1.1 release, based on kotlin's Android UI Toolkit
- In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
- Only learning C can live up to expectations TOP4 S1E6: data type
- Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
- Assembly language segment definition
猜你喜欢
The daemon thread starts redis and modifies the configuration file
ByteDance technical Interviewer: what kind of candidate do I want to pick most
MySQL optimization notes
1. JVM入门介绍
JVM garbage collector part 1
Eight part essay that everyone likes
Log4j2 major vulnerabilities and Solutions
Learn the wisdom of investment Masters
arithmetic operation
8086 CPU internal structure
随机推荐
原型链继承
MySQL date function
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
8086 memory
Prototype chain inheritance
Von Neumann architecture
Activiti directory (III) deployment process and initiation process
唯有学C不负众望 TOP2 p1变量
信息与网络安全期末复习(完整版)
Activiti目录(四)查询代办/已办、审核
Akamai浅谈风控原理与解决方案
【逆向】脱壳后修复IAT并关闭ASLR
唯有学C不负众望 TOP1环境配置
Ruoyi-Cloud 踩坑的BUG
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
MySQL日期函数
Activiti directory (I) highlights
汇编语言寻址方式
arithmetic operation
JVM class loading subsystem