抛出“预期的异常但什么都没有”导致我的测试用例失败。如何解决这个问题。如果在查找阶乘时数字为负,我想抛出异常,
测试文件:
public void testCalculateFactorialWithOutOfRangeException(){
Factorial factorial = new Factorial();
assertThrows(OutOfRangeException.class,
() -> factorial.calculateFactorial(-12));
}
Run Code Online (Sandbox Code Playgroud)
代码文件:
public class Factorial {
public String calculateFactorial(int number) {
//If the number is less than 1
try {
if (number < 1)
throw new OutOfRangeException("Number cannot be less than 1");
if (number > 1000)
throw new OutOfRangeException("Number cannot be greater than 1000");
} catch (OutOfRangeException e) {
}
}
}
public class OutOfRangeException extends Exception {
public OutOfRangeException(String str) {
super(str);
}
}
Run Code Online (Sandbox Code Playgroud)
我预计输出会成功,但它导致了失败