【发布时间】:2022-11-17 18:00:23
【问题描述】:
我无法让此自定义 JUnit 测试正确处理此自定义异常。只是为了帮助自己理清概念,就实际用途而言没有任何用处。无法弄清楚,也找不到任何直接的在线帮助,所以我想在这里问一下。这是怎么回事?
@Test
void testMultiply_WhenFourIsMultipiedByZero_ShouldThrowException() {
int i = 0;
int j = 4;
String expectedMsg = "* by zero";
Exception e = assertThrows(
expectedMsg,
IllegalArgumentException.class, () -> {
tm.multiply(i, j);
});
assertEquals("Error", expectedMsg, e);
// assertEquals(expectedMsg, expectedMsg, e.getMessage()); //this leads to a different error "Method assertEquals(String, Object, Object) is ambiguous for the type"
}
public int multiply(int i, int j) throws Exception {
if(i == 0 || j == 0) {
throw new IllegalArgumentException ("* by zero");
}
return i * j;
}
【问题讨论】: