Reputation: 501
If I have a function a() that can throw exceptions, and I want to mock it. If I write
Mockito.when(a()).callRealMethod()
the compiler tells me to add try-catch block or to add the throw declerations. But I don't want to call the method a() in that line, I just want to make sure that when I call the method somewhere later in the test, it will call the real method, and then I will add try-catch. How to do it?
Upvotes: 1
Views: 1150
Reputation: 95614
It is common practice to add a throws Exception
note to all test methods, as the framework will automatically fail the test if any unexpected exception is thrown. This allows you to call/mock any method that throws an Exception without explicitly writing error-handling code.
For JUnit:
@RunWith(JUnit4.class)
public class YourTest {
@Before public void setUp() throws Exception {
// ...
}
@Test public void fooShouldDoBar() throws Exception {
// ...
}
}
Upvotes: 2