// 自定义的 Math 类
class MyMath {
public static int sum(int a, int b) {
return a + b;
}
public static int sub(int a, int b) {
return a - b;
}
public static int mul(int a, int b) {
return a * b;
}
// 处理除数为零的情况
public static int div(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为零");
}
return a / b;
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
// 测试类
public class MathTest {
@Test
public void testSum() {
System.out.println("testSum");
// 调用自定义的 MyMath 类的 sum 方法
int actual = MyMath.sum(10, 20);
int expected = 30;
Assertions.assertEquals(expected, actual);
}
@Test
public void testSub() {
System.out.println("testSub");
int actual = MyMath.sub(20, 10);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testMul() {
System.out.println("testMul");
int actual = MyMath.mul(2, 5);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDiv() {
System.out.println("testDiv");
int actual = MyMath.div(20, 5);
int expected = 4;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDivByZero() {
System.out.println("testDivByZero");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
MyMath.div(10, 0);
});
}
}
public 是访问修饰符,它表明这个注解可以在任何地方被访问和使用。如果不写 public,注解默认的访问权限是包级私有,也就是只能在同一个包内被访问。
3. @Testable 元注解
@Testable 是一个元注解,元注解的作用是修饰其他注解。在这里,@Testable 对 Test 注解进行了标注,它为 Test 注解添加了额外的元数据或者功能。不过,@Testable 并非 Java 内置的元注解,它应该是自定义的。
五、 JUnit 5 常用注解
JUnit 5 提供了一些常见的测试生命周期注解。
5.1 运行前后的注解
示例
1.@BeforeAll 和@AfterAll
MyMath类:
public class MyMath {
public static int sum(int a, int b) {
return a + b;
}
public static int sub(int a, int b) {
return a - b;
}
public static int mul(int a, int b) {
return a * b;
}
// 处理除数为零的情况
public static int div(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为零");
}
return a / b;
}
}
MathTest类:
import org.junit.jupiter.api.*;
// 测试类
public class MathTest {
@BeforeAll
public static void before(){
System.out.println("所有测试方法执行前运行,仅运行一次(必须是静态方法)");
System.out.println("开始执行单元测试了!");
}
@AfterAll
public static void after(){
System.out.println("所有测试方法执行后运行,仅运行一次(必须是静态方法)");
System.out.println("单元测试执行完毕!");
}
@Test
public void testSum() {
System.out.println("testSum");
// 调用自定义的 MyMath 类的 sum 方法
int actual = MyMath.sum(10, 20);
int expected = 30;
Assertions.assertEquals(expected, actual);
}
@Test
public void testSub() {
System.out.println("testSub");
int actual = MyMath.sub(20, 10);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testMul() {
System.out.println("testMul");
int actual = MyMath.mul(2, 5);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDiv() {
System.out.println("testDiv");
int actual = MyMath.div(20, 5);
int expected = 4;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDivByZero() {
System.out.println("testDivByZero");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
MyMath.div(10, 0);
});
}
}
运行结果:
2.@BeforeEach和@AfterEach
注意:@BeforeEach和@AfterEach 注解的方法不需要是静态的
@BeforeEach
public void beforeEach(){
System.out.println("单元测试方法开始执行");
}
@AfterEach
public void afterEach(){
System.out.println("单元测试方法执行结束");
}
示例代码:
MyMath类:
public class MyMath {
public static int sum(int a, int b) {
return a + b;
}
public static int sub(int a, int b) {
return a - b;
}
public static int mul(int a, int b) {
return a * b;
}
// 处理除数为零的情况
public static int div(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("除数不能为零");
}
return a / b;
}
}
MathTest类:
import org.junit.jupiter.api.*;
// 测试类
public class MathTest {
@BeforeAll
public static void before(){
System.out.println("所有测试方法执行前运行,仅运行一次(必须是静态方法)");
System.out.println("开始执行单元测试了!");
}
@AfterAll
public static void after(){
System.out.println("所有测试方法执行后运行,仅运行一次(必须是静态方法)");
System.out.println("单元测试执行完毕!");
}
@BeforeEach
public void beforeEach(){
System.out.println("单元测试方法开始执行");
}
@AfterEach
public void afterEach(){
System.out.println("单元测试方法执行结束");
}
@Test
public void testSum() {
System.out.println("testSum");
// 调用自定义的 MyMath 类的 sum 方法
int actual = MyMath.sum(10, 20);
int expected = 30;
Assertions.assertEquals(expected, actual);
}
@Test
public void testSub() {
System.out.println("testSub");
int actual = MyMath.sub(20, 10);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testMul() {
System.out.println("testMul");
int actual = MyMath.mul(2, 5);
int expected = 10;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDiv() {
System.out.println("testDiv");
int actual = MyMath.div(20, 5);
int expected = 4;
Assertions.assertEquals(expected, actual);
}
@Test
public void testDivByZero() {
System.out.println("testDivByZero");
Assertions.assertThrows(IllegalArgumentException.class, () -> {
MyMath.div(10, 0);
});
}
}
运行结果:
六、 解决单元测试中 Scanner 失效问题
上述MyMath类:和MathTest类:加入下面代码
@Test
public void testAdd(){
Scanner s=new Scanner(System.in);
int i=s.nextInt();
System.out.println("程序执行到这里了");
}
运行结果:
程序会一直卡在那转圈
解决方法步骤
选中导航栏的 "Help",然后选中 "Edit Custom VM Options...": 在 IntelliJ IDEA 中,"Edit Custom VM Options..." 选项允许你对 Java 虚拟机(JVM)的运行参数进行自定义设置。通过这个操作,你可以打开用于存储自定义 JVM 选项的文件。