2014-10-27 49 views
0

我需要一些帮助来为服务层中的以下方法编写测试。我不确定如何在Mockito中为这项服务模拟这些方法(在DAO以及相同的服务层中)。在此之前,我认为我应该嘲笑整个循环,以避免嘲笑每种方法。为这种方法编写单元测试的正确方法是什么?编写服务功能的单元测试

public List<CheckupHistoryDto> getCaseHistory(Individual patient, Individual doctor) { 
    List<CheckupHistoryDto> checkupHistoryList = new ArrayList<ClaimHistoryDto>(); 
    List<CaseHistory> caseHistoryIds = caseDetailDao.fetchCaseIds(patient.getId(), doctor.getId()); 
    for(CaseHistory caseHistory : caseHistoryIds) { 
     CheckupHistoryDto checkupHistoryDto = new CheckupHistoryDto(); 
     checkupHistoryDto.setDateOfCall(formatter.format(caseHistory.getCreateDate())); 
     checkupHistoryDto.setPatientInfo(getPatientInfo(patient)); 
     checkupHistoryDto.setDoctorInfo(getDoctorInfo(doctor)); 
     checkupHistoryDto.setServiceProvided(caseDetailDao.fetchServiceHistory(caseHistory.getEventId())); 
     checkupHistoryList.add(checkupHistoryDto); 
    } 
    return checkupHistoryList; 
} 

public Patient getPatientInfo(patient) { 
    ... 
} 

public Doctor getDoctorInfo(doctor) { 
    ... 
} 

我的测试用例

@Test 
public void testHistoryList() { 
    Individual patient = Mockito.mock(Individual.class); 
    Individual doctor= Mockito.mock(Individual.class); 
    List<CheckupHistoryDto> checkupHistory = caseService.getCaseHistory(patient, doctor); 
    assertEquals(MOCK_LIST_SIZE, checkupHistory.size()); 
} 

回答

1

忘掉“嘲讽for循环”,这是没有意义的,因为它是你要测试的部分功能;具体来说,当你单元测试XQ类时,你绝不会嘲笑类XQ的任何部分。

你需要模拟如下:

  1. Individual.getId的病人。
  2. Individual.getId为医生。
  3. 对于患者,使用getPatientInfo方法的Individual类中的任何方法。
  4. 对于医生,使用getDoctorInfo方法的Individual类中的任何方法。
  5. caseDetailDao.fetchCaseIDs
  6. caseDetailDao.fetchServiceHistory
  7. caseHistory.getCreateDate,如果返回从fetchCaseIds模拟对象的列表。
  8. caseHistory.getEventId,如果你从fetchCaseIds返回一个模拟对象列表。

你有一些可怕的代码:

  1. caseHsitory.fetchCaseIds显然没有返回caseIDs,它返回情况的详细信息。