java mock void_如何使用Mockito模拟void方法 - How to mock void methods with Mockito

问题:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值