这是我工作正常时的代码
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:
最佳答案
如果您使用参数匹配,则需要将它们用于所有参数。因此,要修复您的测试,您可以使用:
verify(loginService).getUser(eq(loginName), Matchers.<String>eq(null));
或者:
verify(loginService).getUser(eq(loginName), (String) isNull());
或者就个人而言,我会通过简单地使用值为 null
的 userAgent
变量来澄清这一点:
String userAgent = null;
verify(loginService).getUser(eq(loginName), eq(userAgent));
关于java - 为什么我的单元测试在添加参数后失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027952/