下面的简单测试用例失败,但有一个异常(exception)。

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

}

最佳答案

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

简单更改行:

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


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

它应该工作。

关于java - 无效使用参数匹配器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24468456/

10-12 06:25