gpt4 book ai didi

java - 为服务功能编写单元测试

转载 作者:行者123 更新时间:2023-12-01 12:22:16 24 4
gpt4 key购买 nike

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

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());
}

最佳答案

忘记“模拟 for 循环”,这没有任何意义,因为它是您要测试的功能的一部分;具体来说,当您对 XQ 类进行单元测试时,您永远不会模拟 XQ 类的任何部分。

您需要模拟以下内容:

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

你有一些糟糕的代码:

  1. caseHsitory.fetchCaseIds 显然没有返回 caseID,而是返回案例详细信息。

关于java - 为服务功能编写单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com