问题:
How to mock methods with void return type? 如何用void返回类型模拟方法?
I implemented an observer pattern but I can't mock it with Mockito because I don't know how. 我实现了一个观察者模式,但是我不能用Mockito模拟它,因为我不知道怎么做。
And I tried to find an example on the Internet but didn't succeed. 我试图在互联网上找到一个例子,但没有成功。
My class looks like this: 我的课看起来像这样:
public class World {
List 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 getter state
}
public class WorldTest implements Listener {
@Test public void word{
World w= mock(World.class);
w.addListener(this);
...
...
}
}
interface Listener {
void doAction();
}
The system is not triggered with mock. 系统不会通过模拟触发。
I want to show the above-mentioned system state. 我想显示上述系统状态。 And make assertions according to them. 并根据他们做出断言。
解决方案:参考一:
https://stackoom.com/question/9YA3/如何使用Mockito模拟void方法
参考二:
https://oldbug.net/q/9YA3/How-to-mock-void-methods-with-Mockito