What is the best way to mock void methods using Mockito?
I am trying to mock methods that return void
, but I am unsure how to do it with mockito void method.
I implemented an observer pattern, but I can’t seem to mock it correctly using Mockito. I searched for examples but didn’t find a clear solution.
Here’s my class structure:
public class World {
List<Listener> listeners;
void addListener(Listener item) {
listeners.add(item);
}
void doAction(Action goal, Object obj) {
setState("i received");
goal.doAction(obj);
setState("i finished");
}
private String state;
// Setter and getter for state
}
public class WorldTest implements Listener {
@Test
public void testWorld() {
World w = mock(World.class);
w.addListener(this);
// Test logic here
}
}
interface Listener {
void doAction();
}
The system does not trigger properly with mock objects. I want to observe the system state and make assertions based on those states. What is the right approach for mocking void
methods in Mockito?