当前位置:网站首页>Detailed explanation of JUnit unit test framework
Detailed explanation of JUnit unit test framework
2022-07-01 19:25:00 【Gentle ~】
Recommended reading :Java Custom annotation
List of articles
Junit summary
unit testing
Unit testing is to write test code for the smallest functional unit ,Java The smallest functional unit of a program is a method , therefore , Unit testing is for Java Method testing , Then check the correctness of the method .
The disadvantages of manual testing
only one main Method , If the test of a method fails , Other methods of testing will be affected . Unable to get the test result report , The programmer needs to observe whether the test is successful . Unable to automate testing .
Junit Unit test framework
JUnit It's using Java Language implementation of unit testing framework , It's open source ,Java Developers should learn and use JUnit Write unit tests ; Besides , Almost all IDE Tools are integrated JUnit, So that we can go straight to IDE Write and run JUnit test .
JUnit advantage
1.JUnit You can flexibly choose which test methods to perform , The test method can be executed with one key ;
2.Junit You can generate test reports for all methods ;
3. A method test in the unit test failed ( As abnormal ), Tests that do not affect other test methods ;

Quick start
Usage flow
- take JUnit Of jar Package import into project
a. IDEA Usually integrated Junit frame , Generally, there is no need to import ;
b. If IDEA Not integrated well , You need to manually import the following 2 individual JUnit Of jar Package to module ;
- Write test methods : The test method must be a public non static method with no parameters and no return value ;
- Use... On test methods @Test annotation : Note that this method is a test method ;
- Complete the expected correctness test of the tested method in the test method ;
- Check the test method , choice “JUnit function ” , If the test is good, it is green ; If the test fails , It's red ;
To test a method, right-click the method to start the test , Test all methods , You can choose classes or modules to start .
Be careful
The defined test method must be parameterless and return value , And open methods .
Common notes for unit testing
Junit 4.xxxx edition
| annotation | explain |
|---|---|
@Test | The test method |
@Before | Used to modify instance methods , This method is executed once before each test method is executed |
@After | Used to modify instance methods , This method will be executed once after each test method execution |
@BeforeClass | Used to statically decorate methods , This method will be executed only once before all test methods |
@AfterClass | Used to statically decorate methods , This method will be executed only once after all test methods |
Junit 5.xxxx edition
| annotation | explain |
|---|---|
@Test | The test method |
@BeforeEach | Used to modify instance methods , This method is executed once before each test method is executed |
@AfterEach | Used to modify instance methods , This method will be executed once after each test method execution |
@BeforeAll | Used to statically decorate methods , This method will be executed only once before all test methods |
@AfterAll | Used to statically decorate methods , This method will be executed only once after all test methods |
Code example
public class TestUserService {
// Modify the instance method
@Before
public void before(){
System.out.println("===before Method executes once ===");
}
@After
public void after(){
System.out.println("===after Method executes once ===");
}
// Modified static method
@BeforeClass
public static void beforeClass(){
System.out.println("===beforeClass Method executes once ===");
}
@AfterClass
public static void afterClass(){
System.out.println("===afterClass Method executes once ===");
}
/** The test method Be careful : 1、 Must be public , No parameter Methods with no return value 2、 The test method must use @Test Comment mark . */
@Test
public void testLoginName(){
UserService userService = new UserService();
String rs = userService.loginName("admin","123456");
// Test the correctness of the expected results : Assertion .
Assert.assertEquals(" There may be a problem with your login business ", " Login successful ", rs );
}
@Test
public void testSelectNames(){
UserService userService = new UserService();
userService.selectNames();
}
}
public class UserService {
public String loginName(String loginName , String passWord){
if("admin".equals(loginName) && "123456".equals(passWord)){
return " Login successful ";
}else {
return " There is a problem with the user name or password ";
}
}
public void selectNames(){
System.out.println(10/2);
System.out.println(" Query all user names succeeded ~~");
}
}
边栏推荐
- Supervarimag superconducting magnet system SVM series
- pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
- 微服务大行其道的今天,Service Mesh是怎样一种存在?
- Love business in Little Red Book
- Learn MySQL from scratch - database and data table operations
- More information about M91 fast hall measuring instrument
- Getting started with kubernetes command (namespaces, pods)
- MySQL common graphics management tools | dark horse programmers
- The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
- Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
猜你喜欢

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?

Superoptimag superconducting magnet system - SOM, Som2 series

The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables

Getting started with kubernetes command (namespaces, pods)

Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性

Huawei game failed to initialize init with error code 907135000

Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit

Cdga | if you are engaged in the communication industry, you should get a data management certificate

Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?

Junit单元测试框架详解
随机推荐
M91快速霍尔测量仪—在更短的时间内进行更好的测量
Three ways for redis to realize current limiting
Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
【森城市】GIS数据漫谈(一)
Is PMP cancelled??
【快应用】Win7系统使用华为IDE无法运行和调试项目
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
有关 M91 快速霍尔测量仪的更多信息
transform + asm资料
Redis 实现限流的三种方式
Summary of cases of players' disconnection and reconnection in Huawei online battle service
Lumiprobe 亚磷酰胺丨六甘醇亚磷酰胺说明书
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
Cdga | if you are engaged in the communication industry, you should get a data management certificate
3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
宝,运维100+服务器很头疼怎么办?用行云管家!
MFC中如何重绘CListCtrl的表头