Discuss / Java / 异常测试 练习

异常测试 练习

Topic source

保持热爱

#1 Created at ... [Delete] [Delete and Lock User]
public static long fact(long n) {
    if (n < 0) {
        throw new IllegalArgumentException("参数不能小于0");
    }
    if (n > 20) {
        throw new ArithmeticException("参数不能大于20");
    }
    long r = 1;
    for (long i = 1; i <= n; i++) {
        r = r * i;
    }
    return r;
}
@Test
public void testFact() {
    assertThrows(ArithmeticException.class, () -> fact(21L)); 
}

  • 1

Reply