
在软件开发中,确保代码在异常情况下能够正确抛出预期的异常是健壮性测试的重要组成部分。特别是在处理用户输入、资源访问或业务逻辑校验时,我们经常需要验证方法在遇到无效状态时是否抛出特定类型且带有明确信息的异常。
为了更好地说明异常测试,我们首先定义一个简单的Java方法,该方法在特定条件下会抛出ArithmeticException:
public class CalculatorUtils {
/**
* 计算数组中给定数字的倍数数量。
* 如果给定数字为零,则抛出ArithmeticException。
*
* @param number 用于检查倍数的数字。
* @param array 待检查的整数数组。
* @return 数组中给定数字的倍数数量。
*/
public static int getMultiplesOfGivenNumber(int number, int[] array) {
if (number == 0) {
throw new ArithmeticException("Number cannot be zero");
} else {
int multiples = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] % number == 0) {
multiples += 1;
}
}
return multiples;
}
}
// 辅助方法,用于生成测试数据
public static int[] intervalFromOneToTen() {
return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
}我们的目标是测试当number参数为0时,getMultiplesOfGivenNumber方法是否正确抛出ArithmeticException,并验证其错误消息是否为"Number cannot be zero"。
JUnit 4 提供了一个方便的注解参数expected,可以直接在@Test注解中使用,用于声明一个测试方法预期会抛出的异常类型。
import org.junit.Test;
import static org.junit.Assert.fail; // 如果异常未抛出,可使用fail()
public class CalculatorUtilsTest {
// 辅助方法,用于生成测试数据
private int[] intervalFromOneToTen() {
return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
@Test(expected = ArithmeticException.class)
public void testDivideByZeroWithExpectedAnnotation() {
// Arrange
int number = 0;
// Act
// 调用可能抛出异常的方法。如果ArithmeticException被抛出,测试通过。
// 如果未抛出异常,或者抛出了不同类型的异常,测试将失败。
CalculatorUtils.getMultiplesOfGivenNumber(number, intervalFromOneToTen());
// 注意:在此处不需要额外的断言,因为@Test(expected)本身就是一种断言。
// 如果代码执行到这里而没有抛出异常,JUnit会自动使测试失败。
}
}try-catch块是一种更通用、更灵活的异常测试方法,它不仅适用于所有JUnit版本,而且允许你完全控制异常对象,从而可以验证其消息、原因及其他属性。
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; // 用于断言异常未抛出时测试失败
public class CalculatorUtilsTest {
// 辅助方法,用于生成测试数据
private int[] intervalFromOneToTen() {
return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
@Test
public void testDivideByZeroWithTryCatch() {
// Arrange
int number = 0;
try {
// Act
CalculatorUtils.getMultiplesOfGivenNumber(number, intervalFromOneToTen());
// 如果代码执行到这里,说明预期异常没有被抛出,测试应该失败。
fail("Expected ArithmeticException was not thrown.");
} catch (ArithmeticException e) {
// Assert
// 捕获到预期的ArithmeticException,现在可以检查其详细信息。
assertEquals("Number cannot be zero", e.getMessage());
// 如果有其他需要检查的异常属性,也可以在此处进行断言。
} catch (Exception e) {
// 如果抛出了不同类型的异常,也应该失败。
fail("Expected ArithmeticException, but caught a different exception: " + e.getClass().getSimpleName());
}
}
}在try块的末尾,如果代码执行到该位置而没有抛出预期的异常,我们使用fail()方法来明确地使测试失败。fail()方法会抛出一个AssertionError,其中包含你提供的消息,清晰地表明测试未达到预期。
对于使用 JUnit 5 的项目,推荐使用Assertions.assertThrows()方法进行异常测试。它提供了一种更简洁、更功能性的方式来验证异常。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class CalculatorUtilsTest {
// 辅助方法,用于生成测试数据
private int[] intervalFromOneToTen() {
return new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
@Test
void testDivideByZeroWithAssertThrows() {
// 使用assertThrows捕获异常,并返回异常对象
ArithmeticException exception = assertThrows(ArithmeticException.class, () -> {
CalculatorUtils.getMultiplesOfGivenNumber(0, intervalFromOneToTen());
});
// 现在可以对捕获到的异常对象进行断言
assertEquals("Number cannot be zero", exception.getMessage());
}
}assertThrows()方法接收两个参数:预期的异常类型和执行可能抛出异常的代码的Lambda表达式。它会返回捕获到的异常实例,使得后续对异常属性的断言变得非常方便和直观。
在JUnit中测试方法抛出异常是确保代码健壮性的关键步骤。
无论选择哪种方法,核心目标都是确保在特定条件下,你的代码能够按照预期抛出正确类型的异常,并携带准确的错误信息。
以上就是JUnit中如何测试方法抛出特定异常及自定义消息的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号