简体   繁体   English

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

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

Here is my code when working fine 这是我正常工作时的代码

verify(loginService).getUser(eq(loginName));

here it is failing.. 在这里失败了。

@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
    ....
    ....
}

here is the getUser method 这是getUser方法

public LoginModel getUser(String loginName, String userAgent) {
    // userAgent is not being used anywhere
    ....
    return model;
}

Exact Error: 确切错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:

If you're using argument matches, you need to use them for all arguments. 如果使用参数匹配,则需要将它们用于所有参数。 So to fix your test, you can just use: 因此,要修复测试,您可以使用:

verify(loginService).getUser(eq(loginName), Matchers.<String>eq(null));

Or: 要么:

verify(loginService).getUser(eq(loginName), (String) isNull());

Or personally, I'd clarify this by simply having a userAgent variable with a value of null : 或者个人而言,我将通过简单地使用一个值为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