i want stub method call 40 times exception , real object. far can see, mockito 1.10.8's thenthrow()
method accepts n number of throwables:
ongoingstubbing<t> thenthrow(throwable... throwables);
therefore, thought following.
@runwith(mockitojunitrunner.class) public class myobjecttest { @mock(answer = answers.returns_mocks) private mama mama; @mock(answer = answers.returns_deep_stubs) private papa papa; private myobject _instance; @test public void test() { _instance = new myobject(papa, mama); throwable[] exceptions = new throwable[41]; arrays.fill(exceptions, 0, 40, new connectionexception("exception message")); when(papa.getmapper().map(anystring())).thenthrow(exceptions).thenreturn(new mymap()); verify(papa, times(41)).getmapper().map(anystring()); } }
however, when run test following.
org.mockito.exceptions.base.mockitoexception: cannot stub null throwable! @ myobjecttest.test(myobjecttest.java:105)
myobjecttest.java:105 line stubbing takes place.
why error?
you exception because have throwable[]
41 elements, fill 40 of them actual connectionexception
value. last 1 null
.
thenthrow
not accept throwing null
(which cause nullpointerexception
thrown instead).
your array should contain 40 elements
throwable[] exceptions = new throwable[40];
Comments
Post a Comment