Mockito:如何模拟类型`.class`

时间:2018-02-13 10:48:06

标签: java unit-testing mocking mockito

我有我想模拟响应的方法

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

我试着像这样嘲笑它

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

但这不起作用。

如何模拟任何Integer.class

1 个答案:

答案 0 :(得分:3)

您想模拟通话

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))
相关问题