当前位置:网站首页>JUnit unit test
JUnit unit test
2022-06-26 16:33:00 【&volume】
Catalog
1. Test categories :
(1) Black box testing :
No need to write code , Enter value for , See if the program outputs the expected value
Most of them are using black box testing , Is to test according to the code you write
(2) White box testing :
Need to write code , Pay attention to the specific execution process of the program
2.Junit Use :
Junit It's a white box test
(1)@Test Use
1. Define a class
First define the method in the class
package cn.junit;
/*
* Calculator class
* */
public class Calcuator {
/*
* Add
*/
public int add(int a,int b)
{
return a+b;
}
/*
Subtraction
*/
public int sub(int a,int b)
{
return a-b;
}
}
2. Define another test class

package cn.test;
import cn.junit.Calcuator;
import org.junit.Test;
public class CalculatorTest {
/*
test add Method
*/
@Test
public void testAdd()
{
// Create calculator objects
Calcuator c=new Calcuator();
// Calling method
int ad = c.add(1, 2);
System.out.println(ad);
}
}
Just added @Test Because if there is no guide bag, it will become popular
Click on the small light bulb next to it 
Will automatically guide the package 
3. Assertion operations
We often don't output them during the test process , And will assert
Assert.assertEquals( Assertion value , Output value );
package cn.test;
import cn.junit.Calcuator;
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
/*
test add Method
*/
@Test
public void testAdd()
{
// Create calculator objects
Calcuator c=new Calcuator();
// Calling method
int ad = c.add(1, 2);
Assert.assertEquals(3,ad);
// System.out.println(ad);
}
}
If the two values are different , Will pop up and make mistakes
Example of assertion failure :
Example of assertion success :
(2)@Before and @After
[email protected]
@Before To keep the code simple , De duplication operation
@Before At every @Test Block the code that must be executed before annotation , Conduct @Before annotation
Comments will be made in @Test Execute before
[email protected]
@After Existence and @Before It's the same , To keep the code simple , To repeat the operation
@After Is at the end of each @Test Block the code that will be executed after annotation , Conduct @After annotation
Comments will be made in @Test Automatically execute after annotation
3. Code testing
package cn.test;
import cn.junit.Calcuator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest {
@Before
public void testinit()
{
System.out.println("init...");
}
@After
public void close()
{
System.out.println("close.....");
}
/*
test add Method
*/
@Test
public void testAdd()
{
// Create calculator objects
Calcuator c=new Calcuator();
// Calling method
int ad = c.add(1, 2);
//Assert.assertEquals(3,ad);
System.out.println("testadd In progress ");
// System.out.println(ad);
}
/*
* test sub Method
* */
@Test
public void testsub()
{
Calcuator c=new Calcuator();
int sub = c.sub(1, 2);
System.out.println("testsub In progress ");
//System.out.println(sub);
}
}
4. test result
(1) test add Method

(2) test sub Method

3.Junit summary
(1). When defining a test method, the return value should be void type
(2). The parameter list is empty
(3). The verdict If red appears, the test fails
If green appears, the test is successful
(4). Generally, we use assertion operations to process the results
Assert.assertEquals( Assertion value , Output value );
(5)[email protected] Decorating methods are executed before testing methods
(6)[email protected] The decorated method will be executed after the test method is executed ( Whether the test is successful or not, it will be executed );
边栏推荐
- Detailed explanation of cookies and sessions
- How to implement interface current limiting?
- Cuckoo filter for Chang'an chain transaction
- Développer un opérateur basé sur kubebuilder (démarrer)
- 【蓝桥杯集训100题】scratch辨别质数合数 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第15题
- day10每日3题(2):统计最大组的数目
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- Data analysis - numpy quick start
- proxy
- 神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
猜你喜欢

r329(MAIX-II-A(M2A)资料汇总

Develop operator based on kubebuilder (for getting started)

"C language" question set of ⑩

Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction

心情不好,我就这样写代码

The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis

Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
Scala 基础 (二):变量和数据类型

This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
随机推荐
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
JS tutorial - printing stickers / labels using the electronjs desktop application
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
Scala Foundation (2): variables et types de données
Angel 3.2.0 new version released! Figure the computing power is strengthened again
C语言 头哥习题答案截图
【时间复杂度和空间复杂度】
R语言plotly可视化:plotly可视化归一化的直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部边缘使用geom_rug函数添加边缘轴须图
基于Kubebuilder开发Operator(入门使用)
电路中缓存的几种形式
Solution for filtering by special string of microservice
Codeforces Round #802 (Div. 2)
Redis order sorting command
基于STM32+华为云IOT设计的云平台监控系统
IAR工程适配GD32芯片
Practice of federal learning in Tencent micro vision advertising
1-12vmware adds SSH function
并发编程整体脉络
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation