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

如何在java函数中返回预期的异常:java.util.InputMismatchException,java,exception,Java,Exception,这是我的职责 public Object check(long number) { long root = 0; while (number > 0 || root > 9) { if (number == 0) { number = root; root = 0; }

这是我的职责

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);
        }

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

这将不会进入while循环。抛出异常的条件是什么?在方法的开头,如果(number<0)抛出新的inputmaschException(),则需要
if
if (number < 0) throw new InputMismatchException(number + "");
Exception in thread "main" java.util.InputMismatchException: -87625