异常org.mockito.exceptions.verification.TooManyActualInvocations解决方案

今天使用PowerMock写了一段单测代码,

我的代码如下:

Mockito.verify(entitySubscribeDao).addEntitySubscribe(Mockito.any(EntitySubscribe.class));

 

报错如下:

写道
org.mockito.exceptions.verification.TooManyActualInvocations:
entitySubscribeDao.addEntitySubscribe(<any>);
Wanted 1 time:
-> at org.kanpiaoxue.dmeta.service.impl.EntitySubscribeServiceImplTest.testSubscribeEntity(EntitySubscribeServiceImplTest.java:296)
But was 2 times. Undesired invocation:
-> at org.kanpiaoxue.dmeta.service.impl.EntitySubscribeServiceImpl.subscribeEntity(EntitySubscribeServiceImpl.java:251)

 

异常信息说明我的单测代码应该被调用2次,而我实际上只调用了1次。

该如何解决呢?

代码如下:

Mockito.verify(entitySubscribeDao,Mockito.times(2)).addEntitySubscribe(Mockito.any(EntitySubscribe.class));

 参考来自: 

http://stackoverflow.com/questions/14889951/how-to-verify-a-method-is-called-two-times-with-mockito-verify

 

 

内容如下:

 

I want to verify if a method is called twice or more than once through mockito verify. I used verify and it complains like this:

org.mockito.exceptions.verification.TooManyActualInvocations: 
Wanted 1 time:
But was 2 times. Undesired invocation:
share improve this question
 

1 Answer

up vote 109 down vote accepted

Using the appropriate VerificationMode, of course:

verify(mockObject, atLeast(2)).someMethod("was called at least twice");
verify(mockObject, times(3)).someMethod("was called exactly three times");
share improve this answer
 
1 
@Lioan -- You should change the second verify to use the argument times(3). –  Dustin B.  Aug 12 '13 at 18:08 
   
@DustinB. Whoops, of course. Changed it. –  Liosan  Aug 13 '13 at 6:42
1 
For those who don't already have the static import, the full version of times(...) isVerificationModeFactory.times(...). –  Steve Chambers  Aug 27 at 8:03 
   
You can also use Mockito.times(...) instead of VerificationModeFactory.times(...) for the static import –  Wim Deblauwe  Nov 24 at 10:16
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值