我有一些类似的结构
...
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
我该如何处理我的测试用例或设置类型以返回非null值?
您需要将daomock注入MainClass。否则,当调用getDao(AttributeDAO.class)时,它仍将尝试检索真实的dao