I want to know why i need to handle exception ,when i am mocking a void method which throws exception.
For example
public class MyObject {
public void call() throws SomeException {
//do something
}
}
Now when i am doing this,
@Mock
MyObject myObject;
doNothing().when(myObject).call()
it results in compilation error saying
"error: unreported exception SomeException; must be caught or declared to be thrown"
I am wondering , why i need to handle exception for the method, which is itself being mocked .
throws Exception
on your test method. I mark all my JUnit methods withthrows Exception
as a matter of habit - there's no reason not to. – Dawood ibn Kareem@Test(expected = ...)
. I try to avoid thethrows
clause in the test method signature as it does not had any useful information about the test itself. – José Andias