当前位置:网站首页>Junit单元测试框架详解
Junit单元测试框架详解
2022-07-01 18:43:00 【斯文~】
推荐阅读:Java自定义注解
Junit概述
单元测试
单元测试就是针对最小的功能单元编写测试代码,Java程序最小的功能单元是方法,因此,单元测试就是针对Java方法的测试,进而检查方法的正确性。
手动测试的缺点
只有一个main方法,如果一个方法的测试失败了,其他方法测试会受到影响。无法得到测试的结果报告,需要程序员自己去观察测试是否成功。无法实现自动化测试。
Junit单元测试框架
JUnit是使用Java语言实现的单元测试框架,它是开源的,Java开发者都应当学习并使用JUnit编写单元测试;此外,几乎所有的IDE工具都集成了JUnit,这样我们就可以直接在IDE中编写并运行JUnit测试。
JUnit优点
1.JUnit可以灵活的选择执行哪些测试方法,可以一键执行全部测试方法;
2.Junit可以生成全部方法的测试报告;
3.单元测试中的某个方法测试失败了(如异常),不会影响其他测试方法的测试;
快速入门
使用流程
- 将JUnit的jar包导入到项目中
a. IDEA通常整合好了Junit框架,一般不需要导入;
b.如果IDEA没有整合好,需要自己手工导入如下2个JUnit的jar包到模块; - 编写测试方法:该测试方法必须是公共的无参数无返回值的非静态方法;
- 在测试方法上使用@Test注解:标注该方法是一个测试方法;
- 在测试方法中完成被测试方法的预期正确性测试;
- 选中测试方法,选择“JUnit运行” ,如果测试良好则是绿色;如果测试失败,则是红色;
测试某个方法直接右键该方法启动测试,测试全部方法,可以选择类或者模块启动。
注意
定义的测试方法必须是无参数无返回值,且公开的方法。
单元测试常用注解
Junit 4.xxxx版本
注解 | 说明 |
---|---|
@Test | 测试方法 |
@Before | 用来修饰实例方法,该方法会在每一个测试方法执行之前执行一次 |
@After | 用来修饰实例方法,该方法会在每一个测试方法执行之后执行一次 |
@BeforeClass | 用来静态修饰方法,该方法会在所有测试方法之前只执行一次 |
@AfterClass | 用来静态修饰方法,该方法会在所有测试方法之后只执行一次 |
Junit 5.xxxx版本
注解 | 说明 |
---|---|
@Test | 测试方法 |
@BeforeEach | 用来修饰实例方法,该方法会在每一个测试方法执行之前执行一次 |
@AfterEach | 用来修饰实例方法,该方法会在每一个测试方法执行之后执行一次 |
@BeforeAll | 用来静态修饰方法,该方法会在所有测试方法之前只执行一次 |
@AfterAll | 用来静态修饰方法,该方法会在所有测试方法之后只执行一次 |
代码示例
public class TestUserService {
// 修饰实例方法的
@Before
public void before(){
System.out.println("===before方法执行一次===");
}
@After
public void after(){
System.out.println("===after方法执行一次===");
}
// 修饰静态方法
@BeforeClass
public static void beforeClass(){
System.out.println("===beforeClass方法执行一次===");
}
@AfterClass
public static void afterClass(){
System.out.println("===afterClass方法执行一次===");
}
/** 测试方法 注意点: 1、必须是公开的,无参数 无返回值的方法 2、测试方法必须使用@Test注解标记。 */
@Test
public void testLoginName(){
UserService userService = new UserService();
String rs = userService.loginName("admin","123456");
// 进行预期结果的正确性测试:断言。
Assert.assertEquals("您的登录业务可能出现问题", "登录成功", 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 "登录成功";
}else {
return "用户名或者密码有问题";
}
}
public void selectNames(){
System.out.println(10/2);
System.out.println("查询全部用户名称成功~~");
}
}
边栏推荐
猜你喜欢
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
XML语法、约束
MySQL common graphics management tools | dark horse programmers
SuperVariMag 超导磁体系统 — SVM 系列
市值蒸发740亿,这位大佬转身杀入预制菜
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Lumiprobe 亚磷酰胺丨六甘醇亚磷酰胺说明书
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
[live broadcast appointment] database obcp certification comprehensive upgrade open class
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
随机推荐
Database foundation: select basic query statement
Excel之VBA简单宏编程
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Three ways for redis to realize current limiting
Lake Shore 连续流动低温恒温器传输线
Lake Shore M91快速霍尔测量仪
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Summary of cases of players' disconnection and reconnection in Huawei online battle service
3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
Learning notes - steps of JDBC connection database operation
Dlib+Opencv库实现疲劳检测
华为联机对战服务玩家掉线重连案例总结
Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
linux下清理系统缓存并释放内存
华为游戏初始化init失败,返回错误码907135000
js找出数字在数组中下一个相邻的元素