我有一个普通的帮助类,它有我在服务级别类中使用的公共方法。当我为服务类编写测试并试图嘲笑这个助手类的方法之一时,它将进入方法并运行每一行。由于这个方法中的代码更复杂,我想用方法模拟helper类,这样我就不必关心helper类方法中的每个细节。Mockito的嘲笑助手类
服务类
class HistoryServiceImpl implements CaseHistory {
@Override
public List<CaseHistoryDto> getCaseHistory(Individual member, Individual provider) {
MemberUtil memberUtil = new MemberUtil();
List<CaseHistoryDto> caseHistoryDtoList = new ArrayList<CaseHistoryDto>();
List<CaseHistory> caseHistoryList = caseDetailDao.fetchCaseHistory(member.getId(), provider.getId());
for(CaseHistory caseHistory : caseHistoryList) {
CaseHistoryDto caseHistoryDto = new CaseHistoryDto();
caseHistoryDto.setMemberInfo(memberUtil.getMemberInfo(member, caseHistory.getCreateDate()));
caseHistoryDtoList.add(caseHistoryDto);
}
return caseHistoryDtoList;
}
}
测试类
Class HistoryServiceTest {
@Mock MemberUtil memberUtil;
@InjectMocks private HistoryServiceImpl historyServiceImpl = new HistoryServiceImpl();
@Test
public void testGetCaseHistory() {
//why this line going inside real method and executing all lines?
when(memberUtil.getMemberInfo(any(Individual.class), any(Date.class))).thenReturn(member);
}
}
问题是什么? – sfjac 2014-12-01 20:47:16