我有一个相当复杂的测试用例,我正在尝试将以下 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())
这似乎令人费解 - 如果我使用默认值(仅一次调用),它会失败,因为它被多次调用,但如果我告诉它多次调用没问题,它会失败,因为它找不到任何调用!
有人能帮忙吗?