Something tells me I'm doing something wrong...it shouldn't be this hard.
I have a class that relies on some inner class. I'm creating that inner through a protected method so that I can override that in a test to provide a mock. I understand that to get a Mockito mock object to throw an exception from a void method, I have to use doThrow.
But my problem is that the compiler complains that the call to Mockito.doThrow() is throwing RuntimeException...so then I have to add a throws clause to the method setting up the mock. Is that a bug in Mockito itself? doThrow is declaring something that should happen in the future, but not during the setup of the mock.
My OuterClass looks like...
public class OuterClass {
protected InnerClass inner;
protected void createInner() { inner = new InnerClass(); }
protected doSomething() {
try {
inner.go();
} catch (RuntimeException re) {
throw new MyException("computer has caught fire");
}
}
}
And my test code looks like...
@Test(expected = MyException.class)
public void testSomething() {
OuterClass outer = new TestOuterClass();
outer.doSomething();
}
public static class TestOuterClass extends OuterClass {
@Override
public void createInner() {
InnerClass mock = Mockito.mock(InnerClass.mock);
Mockito.doThrow(new RuntimeException("test")).when(mock).go(); // PROBLEM
inner = mock;
}
}