[英]Mockito keep returning null even after stub
我有這樣的結構
...
SomeDao dao = getDAO(AttributeDAO.class);
CustomType type =dao.findByKey(typeOne, typeTwo, typeThree.toString());
if(type == null) {
System.out.print("null returned");
} else {
System.out.print("do something");
}
...
我的測試用例
...
MainClass mc = Mockito.spy(new MainClass());
CustomType type = new CustomType();
SomeDao dao = Mockito.mock(AttributeDAO.class);
type.setValueOne(1);
type.setValueTwo(1);
type.setValueThree("Y");
Mockito.doReturn(type).when(dao).findByKey(1,2,"Y");
mc.callThisDaoFunction();
...
但是每當我嘗試使用 eclemma 覆蓋工具返回類型時,它一直說type == null
我如何我的測試用例或設置類型以返回非空值?
您需要將dao模擬注入MainClass 。 否則在調用getDao(AttributeDAO.class)時它仍然會嘗試檢索真正的dao 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.