当前位置:网站首页>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 ~~");
}
}
边栏推荐
- Shell array
- Lake shore M91 fast hall measuring instrument
- Graduation summary
- Junit单元测试框架详解
- 赋能「新型中国企业」,SAP Process Automation 落地中国
- [6.24-7.1] review of wonderful technical blog posts in the writing community
- Specification of lumiprobe reactive dye indocyanine green
- Contos 7 set up SFTP to create users, user groups, and delete users
- 水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
- 【pytorch记录】模型的分布式训练DataParallel、DistributedDataParallel
猜你喜欢

Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation

pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】

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

Getting started with kubernetes command (namespaces, pods)

Lake Shore M91快速霍尔测量仪

Chaos engineering platform chaosblade box new heavy release

赋能「新型中国企业」,SAP Process Automation 落地中国

从零开始学 MySQL —数据库和数据表操作

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

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
随机推荐
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
Is PMP cancelled??
Docker deploy mysql8.0
Cache problems after app release
Gameframework eating guide
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Netease games, radical going to sea
[to.Net] C set class source code analysis
Transform + ASM data
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
【森城市】GIS数据漫谈(一)
记一次 .NET 差旅管理后台 CPU 爆高分析
【直播预约】数据库OBCP认证全面升级公开课
使用环信提供的uni-app Demo,快速实现一对一单聊
Lumiprobe phosphide hexaethylene phosphide specification
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
MySQL常用图形管理工具 | 黑马程序员
精益思想:来源,支柱,落地。看了这篇文章就懂了
PostgreSQL varchar[] 数组类型操作
Redis 实现限流的三种方式