今天使用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:
|
|
|
|
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");
|
|
answered
Feb 15 '13 at 7:41
|
|
|
|
times(...)
isVerificationModeFactory.times(...)
. – Steve Chambers Aug 27 at 8:03Mockito.times(...)
instead ofVerificationModeFactory.times(...)
for the static import – Wim Deblauwe Nov 24 at 10:16