Java 参数匹配器的无效使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24468456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:22:45  来源:igfitidea点击:

Invalid use of argument matchers

javamockito

提问by Anwar

The simple test case below is failing with an exception.

下面的简单测试用例因异常而失败。

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

I am not sure what is wrong

我不确定出了什么问题

@Test
public void testGetStringTest(){

    final long testId = 1;
    String dlrBAC = null;
    NamedParameterJdbcTemplate jdbcTemplate = mock(NamedParameterJdbcTemplate.class);
    when(this.dao.getNamedParameterJdbcTemplate()).thenReturn(jdbcTemplate);
    when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), String.class
                        )).thenReturn("Test");
    dlrBAC =  dao.getStringTest(testId);
    assertNotNull(dlrBAC);

}

采纳答案by macias

Mockito requires you to either use only raw values or only matchers when stubbing a method call. The full exception (not posted by you here) surely explains everything.

Mockito 要求您在存根方法调用时仅使用原始值或仅使用匹配器。完整的例外(不是你在这里发布的)肯定解释了一切。

Simple change the line:

简单更改行:

when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), String.class
                        )).thenReturn("Test");

to

when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), eq(String.class)
                        )).thenReturn("Test");

and it should work.

它应该工作。