3

I want to stub a method call 40 times with an exception and then with a real object. As far I can see, the Mockito 1.10.8's thenThrow() method accepts n number of Throwables:

OngoingStubbing<T> thenThrow(Throwable... throwables);

Therefore, I thought I could do the 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 I run this test I get the following.

org.mockito.exceptions.base.MockitoException: Cannot stub with null throwable! at MyObjectTest.test(MyObjectTest.java:105)

MyObjectTest.java:105 is the line where the stubbing takes place.

Why do I get this error?

bluesman80
  • 154
  • 3
  • 13
  • If you replace with `thenThrow(new ConnectionException("exception message"))` - does it still throw MockitoException? – jlewkovich Sep 25 '15 at 20:48
  • No, it does not. Actually, one of the other tests in the same test class does that and it passes neatly. – bluesman80 Sep 25 '15 at 20:49
  • 1
    I could not understand what would you need. Could you write more about what you expect? – Fariba Sep 25 '15 at 20:53
  • 1
    @Fariba I want to test if papa.getMapper().map() method is called 41 times. Because, my class under test calls this method as long as a ConnectionException is thrown, but maximum 40 times. – bluesman80 Sep 25 '15 at 20:59
  • @bluesman80 I would check your `exceptions` object is being populated properly with Arrays.fill(). Ensure that the values inside the array are not null – jlewkovich Sep 25 '15 at 21:01
  • I am thinking how to do this, may be this link can give you some idea: http://stackoverflow.com/questions/3498072/how-to-only-throw-exception-when-the-mocked-method-is-called-for-the-first-time – Fariba Sep 25 '15 at 21:07
  • @JLewkovich Good point, thank you. Fariba, I have already checked every question close to mine in this site. But, thanks anyway. – bluesman80 Sep 25 '15 at 21:30

1 Answers1

4

You get this exception because you have a Throwable[] with 41 elements, but you only fill 40 of them with an actual ConnectionException value. The last one is null.

thenThrow does not accept throwing null (which would cause a NullPointerException to be thrown instead).

Your array should only contain 40 elements

Throwable[] exceptions = new Throwable[40];
Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I can't believe that I have been struggling with this for hours! Turns out to be a foolish mistake. Thank you very much! – bluesman80 Sep 25 '15 at 21:28