当前位置:网站首页>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 ~~");
}
}
边栏推荐
- indexof和includes的区别
- Learning notes - steps of JDBC connection database operation
- 组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
- Is PMP cancelled??
- Improve yolov5 with gsconv+slim neck to maximize performance!
- Once the SQL is optimized, the database query speed is increased by 60 times
- Altair HyperWorks 2022 software installation package and installation tutorial
- Docker deploy mysql8.0
- Lumiprobe free radical analysis h2dcfda instructions
- Lumiprobe 自由基分析丨H2DCFDA说明书
猜你喜欢

Love business in Little Red Book

Superoptimag superconducting magnet system - SOM, Som2 series

Lumiprobe 自由基分析丨H2DCFDA说明书

Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增

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

How to use the low code platform of the Internet of things for personal settings?

Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency

Huawei cloud experts explain the new features of gaussdb (for MySQL)

Huawei game failed to initialize init with error code 907135000

洞态在某互联⽹⾦融科技企业的最佳落地实践
随机推荐
transform + asm资料
Taiaisu M source code construction, peak store app premium consignment source code sharing
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
市值蒸发740亿,这位大佬转身杀入预制菜
XML语法、约束
M91快速霍尔测量仪—在更短的时间内进行更好的测量
3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
VBA simple macro programming of Excel
Viewing technological changes through Huawei Corps (VI): smart highway
MFC中如何重绘CListCtrl的表头
Contos 7 搭建sftp之创建用户、用户组以及删除用户
SuperVariMag 超导磁体系统 — SVM 系列
宝,运维100+服务器很头疼怎么办?用行云管家!
Appgallery connect scenario development practice - image storage and sharing
华为联机对战服务玩家掉线重连案例总结
The difference between indexof and includes
Netease games, radical going to sea
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
Today, with the popularity of micro services, how does service mesh exist?