当前位置:网站首页>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
- 241. Different Ways to Add Parentheses
- 智慧防疫系统为建筑工地复工复产提供安全保障
- 案例分享:QinQ基本组网配置
- Transform + ASM data
- Learning notes - steps of JDBC connection database operation
- 6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
- MySQL常用图形管理工具 | 黑马程序员
- kubernetes命令入门(namespaces,pods)
- 宝,运维100+服务器很头疼怎么办?用行云管家!
猜你喜欢

论文阅读【Discriminative Latent Semantic Graph for Video Captioning】

云服务器ECS夏日省钱秘籍,这次@老用户快来领走

Superoptimag superconducting magnet system - SOM, Som2 series

Lake Shore M91快速霍尔测量仪

C端梦难做,科大讯飞靠什么撑起10亿用户目标?

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

Prices of Apple products rose across the board in Japan, with iphone13 up 19%

一次SQL优化,数据库查询速度提升 60 倍

Specification of lumiprobe reactive dye indocyanine green

Huawei cloud experts explain the new features of gaussdb (for MySQL)
随机推荐
How to realize the applet in its own app to realize continuous live broadcast
Junit单元测试框架详解
精益思想:来源,支柱,落地。看了这篇文章就懂了
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
Lake shore M91 fast hall measuring instrument
Once the SQL is optimized, the database query speed is increased by 60 times
案例分享:QinQ基本组网配置
Altair HyperWorks 2022 software installation package and installation tutorial
Lake Shore 连续流动低温恒温器传输线
Specification of lumiprobe reactive dye indocyanine green
使用环信提供的uni-app Demo,快速实现一对一单聊
SuperVariMag 超导磁体系统 — SVM 系列
Dom4J解析XML、Xpath检索XML
华为联机对战服务玩家掉线重连案例总结
Mipi interface, DVP interface and CSI interface of camera [easy to understand]
Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
Helium transmission line of lake shore cryostat
生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度
Appgallery connect scenario development practice - image storage and sharing