为什么我的单元测试在添加参数后失败了?

时间:2014-04-12 08:15:27

标签: java unit-testing

这是我工作正常时的代码

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:

1 个答案:

答案 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));