首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的单元测试在添加参数后失败?

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

Stack Overflow用户
提问于 2014-04-12 08:15:27
回答 1查看 46关注 0票数 1

这是我工作正常时的代码

代码语言:javascript
运行
复制
verify(loginService).getUser(eq(loginName));

在这里它正在失败..。

代码语言:javascript
运行
复制
@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方法

代码语言:javascript
运行
复制
public LoginModel getUser(String loginName, String userAgent) {
    // userAgent is not being used anywhere
    ....
    return model;
}

准确错误:

代码语言:javascript
运行
复制
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-12 08:27:21

如果使用参数匹配,则需要将它们用于所有参数。因此,要修复您的测试,只需使用:

代码语言:javascript
运行
复制
verify(loginService).getUser(eq(loginName), Matchers.<String>eq(null));

或者:

代码语言:javascript
运行
复制
verify(loginService).getUser(eq(loginName), (String) isNull());

或者就我个人而言,我只需要使用一个值为userAgentnull变量来澄清这一点

代码语言:javascript
运行
复制
String userAgent = null;
verify(loginService).getUser(eq(loginName), eq(userAgent));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23027952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档