我有我想要模拟响应的方法
jdbcTemplate.queryForList(query, Integer.class, nick);
我尝试像这样 mock 它
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))
关于java - Mockito:如何模拟类型 `.class`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764979/