11
我有我想添加一個相當複雜的測試用例以下驗證()來:的Mockito驗證()失敗,「太多的實際調用」
verify(userService).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));
這種失敗,此錯誤:
org.mockito.exceptions.verification.TooManyActualInvocations:
userService.getUserById(<any>);
Wanted 1 time:
-> at test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)
But was 4 times. Undesired invocation:
所以我改成了這樣:
verify(userService, atLeastOnce()).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));
而現在它失敗:
java.lang.NullPointerException
at test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)
因爲此函數返回空值:
verify(userService, atLeastOnce()).getUserById(anyLong())
這似乎令人費解 - 如果我使用默認值(僅一個調用),它失敗,因爲它被調用多次,但如果我告訴它多調用是可以的,它失敗了,因爲它找不到任何調用!
任何人都可以幫忙嗎?
你試試用具體的數字? 'verify(userService,times(4))...' –