How to mock void methods with Mockito

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 find an example on the Internet but didn’t succeed. My class looks like this: public class World { List<Listener> listeners; void addListener(Listener item) { listeners.add(item); } … Read more

Mocking static methods with Mockito

Use PowerMockito on top of Mockito. Example code: @RunWith(PowerMockRunner.class) @PrepareForTest(DriverManager.class) public class Mocker { @Test public void shouldVerifyParameters() throws Exception { //given PowerMockito.mockStatic(DriverManager.class); BDDMockito.given(DriverManager.getConnection(…)).willReturn(…); //when sut.execute(); // System Under Test (sut) //then PowerMockito.verifyStatic(); DriverManager.getConnection(…); } More information:

throw checked Exceptions from mocks with Mockito

Check the Java API for List.The get(int index) method is declared to throw only the IndexOutOfBoundException which extends RuntimeException.You are trying to tell Mockito to throw an exception SomeException() that is not valid to be thrown by that particular method call. To clarify further.The List interface does not provide for a checked Exception to be thrown from the get(int index) method and that is why Mockito is failing.When … Read more