这是我工作正常时的代码
verify(loginService).getUser(eq(loginName));
这里失败了..
@Test
public void test_getUserFlow4() {
...
LoginModel loginModelReturned = loginService.getUser(loginName, null);
assertGeneralConditions(loginModelReturned);
...
}
private void assertGeneralConditions(LoginModel loginModelReturned){
verify(loginService).getUser(eq(loginName), null); //test failed here other lines not executed
....
....
}
这是getUser方法
public LoginModel getUser(String loginName, String userAgent) {
// userAgent is not being used anywhere
....
return model;
}
确切错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
答案 0 :(得分:2)
如果您正在使用参数匹配,则需要将它们用于所有参数。所以要修复你的测试,你可以使用:
verify(loginService).getUser(eq(loginName), Matchers.<String>eq(null));
或者:
verify(loginService).getUser(eq(loginName), (String) isNull());
或者就个人而言,我只需要一个userAgent
变量,其值为null
即可澄清这一点:
String userAgent = null;
verify(loginService).getUser(eq(loginName), eq(userAgent));