当前位置:网站首页>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("查询全部用户名称成功~~");
}
}
边栏推荐
- 助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
- kubernetes命令入门(namespaces,pods)
- June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
- Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
- 2020, the regular expression for mobile phone verification of the latest mobile phone number is continuously updated
- Three simple methods of ES6 array de duplication
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- Altair HyperWorks 2022软件安装包和安装教程
- Lumiprobe cell imaging study PKH26 cell membrane labeling kit
- Games202 operation 0 - environment building process & solving problems encountered
猜你喜欢

精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会

MySQL common graphics management tools | dark horse programmers

XML语法、约束

CDGA|从事通信行业,那你应该考个数据管理证书

Lumiprobe cell imaging study PKH26 cell membrane labeling kit

Love business in Little Red Book

11、用户、组和权限(1)

中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒

机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
随机推荐
Lake Shore M91快速霍尔测量仪
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
Learning notes - steps of JDBC connection database operation
Netease games, radical going to sea
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
More information about M91 fast hall measuring instrument
JS find the next adjacent element of the number in the array
Games202 operation 0 - environment building process & solving problems encountered
PriorityQueue的用法和底层实现原理
ES6数组方法find()、findIndex()的总结「建议收藏」
Huawei game failed to initialize init with error code 907135000
CDGA|从事通信行业,那你应该考个数据管理证书
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享
数据库基础:select基本查询语句
Qfile read / write file operation in QT
实例讲解将Graph Explorer搬上JupyterLab
Lefse analysis
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
VBA simple macro programming of Excel
Is PMP cancelled??