繁体   English   中英

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

[英]why my unit test failing after adding a parameter?

这是我正常工作时的代码

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());

或者个人而言,我将通过简单地使用一个值为nulluserAgent变量来澄清这一点:

String userAgent = null;
verify(loginService).getUser(eq(loginName), eq(userAgent));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM