簡體   English   中英

參數匹配器的使用無效

[英]Invalid use of argument matchers

下面的簡單測試用例因異常而失敗。

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

它應該工作。

對我來說,我使用的是 EasyMock,這是錯誤的導入。 確保您正確導入了 (anyString, any, anyInt)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM