2

I have a method

Service.class

Public void foo() throws SchedulerException{ .......... } 

Test.class

Public void testExample(){ Mockito.doThrow(SchedulerException.class).when(f.foo()); } 

Compiler is asking me to put either a try/catch or throws for testExample() Is there any other way to achieve the same ?

7
  • I assume that SchedulerException does not extends RuntimeException? In this case, your test-method has to declare throws SchedulerExcpetion. There is no other way to achieve what you want since Java enforces the catch-or-throws principle. the only exception (pun) to this are RuntimeExceptions (because nobody really wants to enclose each and every array-access in a try-catch-block...) Commented May 1, 2017 at 17:24
  • 1
    And just for annotation purposes, also potentially add @Test (expected = SchedulerException.class) if the above is the case. Commented May 1, 2017 at 17:26
  • If the code inside the test throws exception it means that the test fails, so it is better to put throws SchedulerException to testExample (you don't need to put it if your exception extends RuntimeException): the test will fail in case of throwing the exception. If the purpose of the test is to test that the exception is thrown, you would you use try-catch, but I guess it is not your case. Commented May 1, 2017 at 17:26
  • @VasiliyVlasov Just because your test threw an exception does not mean that the test failed. Some tests are designed to trigger certain exceptions. Commented May 1, 2017 at 17:28
  • @Turing85 Then you would wrap the code in try-catch and call something like fail() after the tested code but before catch, right? That's what I meant by "If the purpose of the test is to test that the exception is thrown, you would you use try-catch, but I guess it is not your case." But anyway thanks for your valuable remark. Commented May 1, 2017 at 17:52

1 Answer 1

1

Your question did not specify whether SchedulerException extends RuntimeException, but from your question I assume it does not.

Your test method (testExample()) has to throws SchedulerException if SchedulerException does not extends RuntimeException. This is due to Java's nature to enforce the catch-or-throws principle. The details can be found in JLS §11.

As was suggested by @Ishnark, if you expect your test to throw any kind of Exception, you should annotate the test with @Test(expected=ExcpectedExceptin.class) (even if your exception extends RuntimeExcpetion).

Sign up to request clarification or add additional context in comments.

1 Comment

Just to "re-phrase" the answer above. If a method invoke something that can throw an exception. you NEED to do one of the following (except if the exception extends RuntimeException) Either you add it to the method signature (which will be thrown to the class it has been called from), or you try/catch and deal with the exception here. In the case of a test. Junit WILL fail any test if an exception is thrown. As Turing85 said, If it is the case Junit annotation @Test accept an argument which is expected=TheExceptionThatShouldBeThrown.class Test will passe only if exception is thrown

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.