[英]testing service with mockito and spring
尝试测试服务方法。 这是我要测试的方法:
@Override
public ReportListDto retrieveAllReportsList() {
List<ReportDto> reportDtos = reportMapper.toDtos(reportRepository.findAll());
return new ReportListDtoBuilder().reportsDto(reportDtos).build();
}
这是我的测试方法(我从一些教程中获得了它):
@Test
public void testRetrieveAllReportList() throws Exception {
List<Report> expected = new ArrayList<>();
when(reportRepositoryMock.findAll()).thenReturn(expected);
ReportListDto actual = reportService.retrieveAllReportsList();
verify(reportRepositoryMock, times(1)).findAll();
verifyNoMoreInteractions(reportRepositoryMock);
assertEquals(expected, actual);
}
但是教程不使用DTO模型。 因此,最后一个声明具有预期的List<Report>
对象和实际的ReportListDto
对象。
这是我的ReportListDto
:
public class ReportListDto implements Serializable {
private List<ReportDto> reports = new ArrayList<>();
public List<ReportDto> getReports() {
return reports;
}
public void setReports(List<ReportDto> reports) {
this.reports = reports;
}
}
如何测试服务使用dto映射器的功能?
如果在ReportListDTO
对象上实现equals
,则assertEquals
将使用它。
像intelliJ或Eclipse这样的许多IDE都可以为您完成此操作...否则,您可以编写如下内容:
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ReportListDto that = (ReportListDto) o;
return reports != null ? reports.equals(that.reports) : that.reports == null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.