`
kanpiaoxue
  • 浏览: 1745681 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

异常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:
shareimprove this question
 
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
 
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
分享到:
评论

相关推荐

    mockito-core-4.0.0-API文档-中英对照版.zip

    Maven坐标:org.mockito:mockito-core:4.0.0; 标签:core、mockito、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    mockito-junit-jupiter-3.3.3.jar中文-英文对照文档.zip

    注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...

    mockito-core-4.0.0-API文档-中文版.zip

    Maven坐标:org.mockito:mockito-core:4.0.0; 标签:core、mockito、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...

    powermock-api-mockito-common-1.6.6.jar

    java运行依赖jar包

    mockito-core-2.15.0-API文档-中文版.zip

    Maven坐标:org.mockito:mockito-core:2.15.0; 标签:core、mockito、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...

    mockito-core-3.8.0-API文档-中文版.zip

    Maven坐标:org.mockito:mockito-core:3.8.0; 标签:mockito、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...

    mockito-core-3.9.0-API文档-中文版.zip

    Maven坐标:org.mockito:mockito-core:3.9.0; 标签:mockito、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...

    mockito-core-3.8.0-API文档-中英对照版.zip

    Maven坐标:org.mockito:mockito-core:3.8.0; 标签:mockito、core、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    mockito-core-3.1.0-API文档-中英对照版.zip

    Maven坐标:org.mockito:mockito-core:3.1.0; 标签:mockito、core、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    mockito-core-2.15.0-API文档-中英对照版.zip

    Maven坐标:org.mockito:mockito-core:2.15.0; 标签:core、mockito、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档...

    mockito-core-3.1.0-API文档-中文版.zip

    Maven坐标:org.mockito:mockito-core:3.1.0; 标签:mockito、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和...

    spring入门好教程

    spring 总的概述。。。入门好教程~~~~~~~~~~~~~

    mockito-jar.zip

    mockito-all-1.10.19.jar mockito-core-4.5.1.jar mockito-junit-jupiter-4.5.1.jar mockito-junit-jupiter-4.5.1-sources.jar

    Java-Junit-Mockito

    Java-Junit-Mockito 课程概述步骤列表JUnit步骤01:需要单元测试步骤02:设置您的第一个JUnit步骤03:第一个成功的JUnit。 Green Bar和assertEquals步骤04:重构您的第一个JUnit测试步骤05:第二个JUnit示例...

    Mockito-Programming-Cookbook

    Mockito is an open source testing framework for Java released under the MIT License. The framework allows the creation of test double objects (mock objects) in automated unit tests for the purpose of ...

    Packt Publishing Mockito Essentials (2014)

    You will also gain an insight on the concepts of Service-oriented Architecture (SOA) as well as understand how to unit test RESTful web services with Mockito. By sequentially working through the ...

    mockito-core-3.6.0.jar

    mockito-core-3.6.0.jarmockito-core-3.6.0.jar

    mockito-core-2.23.4.jar

    mockito-core-2.23.4.jar

    NlpWithNeuralNetwork:Google IO扩展事件演示

    4.12 ) H2数据库引擎( com.h2database:h2:1.4.196 ) Mockito( org.mockito:mockito-core:2.8.47 ) Hibernate ORM( org.hibernate:hibernate-core:5.2.10.Final , org.hibernate:hibernate-entitymanager:...

Global site tag (gtag.js) - Google Analytics