参数匹配器的使用无效
下面的简单测试用例失败,但有例外。
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要求您在存根方法调用时仅使用原始值或仅使用匹配器。 完整的例外(这里没有发布)肯定会解释一切。
简单改变一下:
when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), String.class )).thenReturn("Test");
至
when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), eq(String.class) )).thenReturn("Test");
它应该工作。