无效使用参数匹配器

发布于 2021-01-30 17:18:24

下面的简单测试案例失败了,但有一个例外。

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

我不知道怎么了

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

}
关注者
0
被浏览
95
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    Mockito要求您在存入方法调用时仅使用原始值或仅使用匹配器。完整的例外情况(您未在此处发布)肯定可以解释所有情况。

    简单更改行:

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

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

    它应该工作。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看