gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:56:33 24 4
gpt4 key购买 nike

这是我工作正常时的代码

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

关于java - 为什么我的单元测试在添加参数后失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com