如何在我的java函数中返回预期的异常:java.util.InputMismatchException



这是我的函数

public Object check(long number) {
long root = 0;
while (number > 0 || root > 9) {
if (number == 0) {
number = root;
root = 0;
}
root += number % 10;
number /= 10;
}
return root;
}

**我想通过这个测试用例,我如何才能解决它**

@Test(expected = InputMismatchException.class)
public void testInvalidInput() {
digitalRoot.check(-87625L);
}
if (number < 0) throw new InputMismatchException(number + "");

将其添加为方法的第一行,它将抛出所需的异常。

Exception in thread "main" java.util.InputMismatchException: -87625

您可以更改错误消息以提供更多上下文。

最新更新