我有一个相当复杂的测试用例,我正在尝试将以下 verify() 添加到:
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)
因为这将返回 null:
verify(userService, atLeastOnce()).getUserById(anyLong())
这似乎令人费解——如果我使用默认值(仅一次调用),它会失败,因为它被多次调用,但如果我告诉它多次调用没问题,它就会失败,因为它找不到任何调用!
有人能帮忙吗?
原文由 user1071914 发布,翻译遵循 CC BY-SA 4.0 许可协议
看起来你们都想模拟调用
userService.getUserById()
时发生的情况,并且还要验证setPasswordChangeRequired(true)
在返回的对象上调用。您可以通过以下方式完成此操作: