简体   繁体   中英

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

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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