Java Mockito 不允许 Matchers.any() 与 Integer.class
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21441551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito not allowing Matchers.any() with Integer.class
提问by eggshell
I am trying to unit test this method:
我正在尝试对这种方法进行单元测试:
/**
* finds all widget descriptions containing specified text
* @param searchText
* @return
*/
@Transactional
public List<Integer> returnWidgetIdsFromSearchWord(String searchText){
List<Integer> widgetIds = new ArrayList<Integer>();
MapSqlParameterSource args = new MapSqlParameterSource();
try{
widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
+ "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
}catch(Exception e){
}
return widgetIds;
}
with this JUnit test:
使用此 JUnit 测试:
@Test
public void testReturnWidgetIdsFromSearchWord(){
List<Integer> widgetIds = null;
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
Matchers.any(Integer.class))).thenReturn(idList);
widgetIds = (List<Integer>) dDao.returnWidgetIdsFromSearchWord("someText");
assertEquals(widgetIds, idList);
}
I have tried just use Integer.class without the Matcher - no luck because then it complains about needing 3 matchers. Any suggestions? And thanks
我试过只使用没有匹配器的 Integer.class - 没有运气,因为它抱怨需要 3 个匹配器。有什么建议?谢谢
采纳答案by eggshell
I changed my test to this and it worked:
我把我的测试改成了这个,它奏效了:
@Test
public void testReturnWidgetIdsFromSearchWord(){
List<Integer> widgetIds = null;
String searchText = "someText";
/*when(jdbt.queryForList("SELECT idwidgets FROM descriptions "
+ "WHERE descriptiontext LIKE '%"+ searchText + "%'",
args, Integer.class)).thenReturn(idList);*/
when(jdbt.queryForList(Matchers.anyString(), Matchers.any(MapSqlParameterSource.class),
(Class<Integer>) Matchers.anyVararg())).thenReturn(idList);
widgetIds = (List<Integer>) dDao.returnWidgetIdsFromSearchWord(searchText);
System.out.println(widgetIds.size());
assertEquals(widgetIds, idList);
}
Thanks for the help
谢谢您的帮助
回答by Peter Keller
If you need to mock NamedParameterJdbcTemplate#queryForList(String, SqlParameterSource, Class)then just use
如果您需要模拟NamedParameterJdbcTemplate#queryForList(String, SqlParameterSource, Class)那么只需使用
when(jdbt.queryForList(Matchers.anyString(), Matchers.any(SqlParameterSource.class), Matchers.any(Class.class))).thenReturn(idList);
Is it possible that you didn't pass your template object to the DAO instance? Find my full test class below. It passes the test successfully:
您是否可能没有将模板对象传递给 DAO 实例?在下面找到我的完整测试课程。它成功通过了测试:
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
public class DebugTest {
private MyDao dDao;
private final NamedParameterJdbcTemplate jdbt = mock(NamedParameterJdbcTemplate.class);
@SuppressWarnings("unchecked")
@Test
public void testReturnWidgetIdsFromSearchWord() {
final List<Integer> idList = new ArrayList<Integer>();
this.dDao = new MyDao(this.jdbt);
when(this.jdbt.queryForList(anyString(), any(SqlParameterSource.class), any(Class.class)))
.thenReturn(idList);
final List<Integer> widgetIds = this.dDao.returnWidgetIdsFromSearchWord("Hallo");
assertEquals(widgetIds, idList);
}
private static class MyDao {
private final NamedParameterJdbcTemplate jdbt;
public MyDao(final NamedParameterJdbcTemplate jdbt) {
this.jdbt = jdbt;
}
public List<Integer> returnWidgetIdsFromSearchWord(final String searchText) {
List<Integer> widgetIds = new ArrayList<Integer>();
SqlParameterSource args = new MapSqlParameterSource();
try {
widgetIds = (List<Integer>) jdbt.queryForList("SELECT idwidgets FROM descriptions "
+ "WHERE descriptiontext LIKE '%"+ searchText + "%'", args, Integer.class);
} catch(Exception e) {
}
return widgetIds;
}
}
}
回答by MariuszS
Do not cast Matchers.anyVararg()
, there is better solution.
不要投Matchers.anyVararg()
,有更好的解决方案。
Method queryForList
has signature
方法queryForList
有签名
queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType)
so instead of
所以而不是
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
Matchers.any(Integer.class))).thenReturn(idList);
use
用
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
Matchers.<Class<Integer>>any())).thenReturn(idList);
as described in Mockito: Verifying with generic parameters
Do not use code with anyVararg()
and casting
不要使用带有anyVararg()
和强制转换的代码
when(jdbt.queryForList(Matchers.anyString(),
Matchers.any(MapSqlParameterSource.class),
(Class<Object>) Matchers.anyVararg()).thenReturn(idList);
because this generate warning
因为这会产生警告
Unchecked cast: `java.lang.Object` to `java.lang.Class<java.lang.Object>`