今天使用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:

junit mockito verify
shareimprove this question
edited Sep 15 at 12:57
Ripn Al Wasim

12.1k1772102

asked Feb 15 '13 at 7:23
Ahmad Beg

310145

 
add a comment

1 Answer

activeoldestvotes

up vote109down voteaccepted

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");

shareimprove this answer
edited Jan 6 at 11:36
Aniket Thakur

19.9k1181109

answered Feb 15 '13 at 7:41
Liosan

1,2941713

 
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

异常org.mockito.exceptions.verification.TooManyActualInvocations解决方案相关推荐

  1. ios cannot use “@throw“ with objective-c exceptions disabled 问题解决方案

    ios cannot use "@throw" with objective-c exceptions disabled 问题解决方案 参考文章: (1)ios cannot us ...

  2. java.lang.IllegalStateException异常:简单的分析和简单解决方案

    java.lang.IllegalStateException异常:简单的分析和简单解决方案 参考文章: (1)java.lang.IllegalStateException异常:简单的分析和简单解决 ...

  3. maven怎么强制updating_Eclipse中maven异常Updating Maven Project的统一解决方案

    现在使用maven的公司和团队越来越多,虽然没有Gradle那么灵活,但是现对于以前的项目构建方式还是很有优势的,下面分享一个maven update 时的异常统一解决方案: 异常案例: 1.An i ...

  4. Exception in thread “main“ org.apache.ibatis.exceptions.PersistenceException奇葩解决方案

    项目场景: Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Er ...

  5. 进程异常行为-访问恶意下载源 解决方案_不要忽略服务器上的异常进程

    在之前的文章中,我们看到了ADAudit Plus的用户行为分析(UBA)功能如何使管理员能够监视用户登录活动以识别受感染的帐户.ADAudit Plus中的UBA还可以帮助您跟踪成员服务器上的任何异 ...

  6. java制造null异常_Java中NullPointerException的完美解决方案

    null在Java中带来的麻烦 我相信所有的Java程序猿一定都遇到过NullPointerException,空指针在Java程序中是最常见的,也是最烦人的:它让我们很多程序猿产生了根深蒂固的感觉, ...

  7. 应用程序错误:应用程序发生异常未知的软件异常(0x0eedfade),位置为0x7c812aeb解决方案

    [问题描述]:      弹出应用程序: Explorer.EXE - 应用程序错误: 应用程序发生异常 未知的软件异常 (0x0eedfade),位置为 0x7c812aeb [解决方案]:     ...

  8. Python代理IP异常:requests.exceptions.ProxyError: HTTPSConnectionPool(host=‘xxxx‘, port=443)

    背景 这是我在使用Python爬取网页信息的时候出现的异常. 由于很多要爬取的网站会把"IP限制"作为反爬虫手段,所以我偶尔会使用"代理IP服务器"去处理相关需 ...

  9. java+selenium报异常org.openqa.selenium.StaleElementReferenceException的解决方案

    为什么80%的码农都做不了架构师?>>>    因为页面内容有很多页,需要切换页数,但是切换跳转到第二页的时候,页面首先会自动刷新,导致出现如下异常:Exception in thr ...

最新文章

  1. 图像数据增强(平移,旋转,亮度变换,翻转,添加高斯噪声,缩放,裁剪)
  2. JavaScript核心语法学习部分(四)
  3. Android之子菜单的创建
  4. python set使用
  5. linux查看进程相关命令
  6. 拿到两封信,开心ING
  7. 【Go API 开发实战】Go API 开发实战教程简介(1-7)
  8. SharePoint 2007有性能问题? 先试试这篇.
  9. Android多点触摸 与 手势识别
  10. 2013=726 整合,优化,利用自身资源。 让自己的时间更有意义,最大化利用
  11. db9串口(db9串口定义及颜色)
  12. VOC2007和VOC2012数据集下载地址
  13. java ftp 250状态码_FTP状态码
  14. 华为MateBook电脑PAK-AL09救砖教程
  15. Web全栈工程师到底是个什么鬼?为什么Facebook声称:“我们只招全栈工程师!”
  16. 数组,向量和矩阵以及空间的维数
  17. xmind可以画流程图吗_Xmind制作流程图教程 xmind怎么画流程图?xmind画流程图步骤...
  18. Python之dir()与__dict__的区别
  19. 使用python绘制一个渐变色的圆
  20. 微信小程序阶段总结一

热门文章

  1. js 百度地图标记定位(一)
  2. DRM框架(vkms)分析(4)----encoder初始化
  3. 【skLearn 回归模型】岭回归 <linear_model.Ridge>
  4. Python网络爬虫之HTTP原理
  5. 自定义时间(小时:分钟)选择器
  6. Hadoop(三)通过C#/python实现Hadoop MapReduce
  7. 如何用Windows命令提示符(cmd.exe)进入指定目录
  8. WPF的Prism框架简介
  9. Windows Service 创建与安装
  10. 单线程与多线程使用场景