How to mock void methods with Mockito

前端 未结 9 2207
情话喂你
情话喂你 2020-11-22 12:21

How to mock methods with void return type?

I implemented an observer pattern but I can\'t mock it with Mockito because I don\'t know how.

And I tried to fin

9条回答
  •  耶瑟儿~
    2020-11-22 13:08

    I think your problems are due to your test structure. I've found it difficult to mix mocking with the traditional method of implementing interfaces in the test class (as you've done here).

    If you implement the listener as a Mock you can then verify the interaction.

    Listener listener = mock(Listener.class);
    w.addListener(listener);
    world.doAction(..);
    verify(listener).doAction();
    

    This should satisfy you that the 'World' is doing the right thing.

提交回复
热议问题