繁体   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