进阶day12-Junit测试框架概述

This commit is contained in:
2026-04-01 10:45:49 +08:00
parent fcd7b9e1b4
commit 2fff7071b7

View File

@@ -0,0 +1,74 @@
package com.inmind.junit01;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/*
Junit测试框架
框架就是将一些工具类抽象父类接口以及它们的实现类封装到一个jar包帮助我们程序员快速高效地实现一些功能
Junit测试框架帮助程序员快速高效测试功能代码
junit测试框架的使用步骤
1.先定义出不同的功能方法,将要测试的代码放入各自方法中
2.在要测试的功能方法上添加注解@Test
3.可以在方法中直接右键运行或者方法外右键运行全部
Junit测试框架是一个第三方的框架如果要使用非JDK提供的功能必须要导包。
junit测试框架的常用注解
a.@Test:设置当前方法是否要进行测试
b.@Before: 设置测试每个方法之前,要添加的执行代码
c.@After: 设置测试每个方法之后,要添加的执行代码
junit框架的注意事项
1.方法必须是public
2.必须是无参
3.必须无返回值
junit测试框架如何实现的
注解(自定义注解)+反射(识别注解+解析注解)
*/
public class Demo01 {
long start;
@Before
public void before(){
//获取系统时间
System.out.println("功能测试开始了");
start = System.currentTimeMillis();
}
@After
public void after(){
//获取系统时间
System.out.println("功能测试结束了");
long end = System.currentTimeMillis();
System.out.println("当前功能方法消耗了"+(end - start)+"毫秒");
}
@Test
public void method1(){
//1.测试功能
int a = 1/1;
}
@Test
public void method2(){
//2.测试功能
int[] arr = {1, 2, 3};
System.out.println(arr[2]);
}
@Test
public void method3(){
//3.测试功能
System.out.println("这是一个测试功能");
}
}