1

I have to test a method which invokes two void methods. I just want to check if the two void methods are invoked or not, but the method must be stubbed.

How do I do it? I tried to implement it using Mockito doThrow method, but not success.

doThrow(new RuntimeException()).when(mockedClass).methodName();

Wanted but not invoked: error

How do I solve my problem?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Gagan
  • 4,278
  • 7
  • 46
  • 71

2 Answers2

3

You can verify only calls in mocked stuff, e.g.

    Foo bar = Mockito.mock(Foo.class);
    ClassToTest testInstance = new ClassToTest(bar);

    testInstance.doStuff();
    Mockito.verify(bar, times(1)).someMethod(); // will pass if someMethod of Foo class was called in scope of testInstance.doStuff()

I'm not really sure that you should check actual method calls by expecting an exception. Could you provide some code/a bit more details about the context?

alexk
  • 59
  • 9
1

You actually need to use verifyMethod on Mockito. Here is someone who had the same issue. The example shown mocks the object, injects it and then checks to see if it was called or not.

Mockito : how to verify method was called on an object created within a method?

Community
  • 1
  • 1
Dale
  • 1,613
  • 4
  • 22
  • 42