当前位置:网站首页>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 ~~");
}
}
边栏推荐
- 物联网平台thingsboard搭建学习记录
- C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- MySQL common graphics management tools | dark horse programmers
- Viewing technological changes through Huawei Corps (VI): smart highway
- 【森城市】GIS数据漫谈(一)
- Love business in Little Red Book
- 精益思想:来源,支柱,落地。看了这篇文章就懂了
- Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
- Gameframework eating guide
猜你喜欢
Getting started with kubernetes command (namespaces, pods)
Cdga | if you are engaged in the communication industry, you should get a data management certificate
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
市值蒸发740亿,这位大佬转身杀入预制菜
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
How to realize the applet in its own app to realize continuous live broadcast
智慧防疫系统为建筑工地复工复产提供安全保障
Prices of Apple products rose across the board in Japan, with iphone13 up 19%
从零开始学 MySQL —数据库和数据表操作
小红书上的爱情买卖
随机推荐
Go语言高级
Shell array
Specification of lumiprobe reactive dye indocyanine green
Three ways for redis to realize current limiting
AppGallery Connect场景化开发实战—图片存储分享
Nacos configuration file publishing failed, please check whether the parameters are correct solution
论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
Lumiprobe phosphide hexaethylene phosphide specification
Example explanation: move graph explorer to jupyterlab
【pytorch记录】模型的分布式训练DataParallel、DistributedDataParallel
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
Transform + ASM data
Solution: you can ping others, but others can't ping me
Lake Shore - crx-em-hf low temperature probe station
微服务大行其道的今天,Service Mesh是怎样一种存在?
学习笔记-JDBC连接数据库操作的步骤
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
【快应用】Win7系统使用华为IDE无法运行和调试项目
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数