簡體   English   中英

Mockito:如何模擬`.class`類型

[英]Mockito: how to mock type `.class`

我有想要模擬響應的方法

jdbcTemplate.queryForList(query, Integer.class, nick);

我試着這樣嘲笑

doReturn(Collections.singletonList(1))
            .when(jdbcTemplate)
            .queryForList(anyString(), any(Integer.class), anyString());

但這是行不通的。

我如何模擬任何Integer.class

您想模擬通話

template.queryForList(String s, Class<T> elementType, Object... args);

所以你需要做

when(template)
   .queryForList(anyString(), any(Class.class), any(Object[].class)
   .thenReturn(1);

您應該減少any使用,最好這樣做

when(template)
    .queryForList("sql", Integer.class, "yourArg")

或結合

when(template)
    .queryForList(eq("sql"), eq(Integer.class), any(Object[].class))

暫無
暫無

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

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