【发布时间】:2021-05-05 14:39:54
【问题描述】:
你好,我正在写一个测试用例
@Override
public SnapDocument updateFlights(SnapDocument snapDocument, FlightListingResponse flightsResponse) {
HashMap<String, FaresInfo> outboundFlights = new HashMap<>();
HashMap<String, FaresInfo> inboundFlights = new HashMap<>();
Objects.requireNonNull(flightsResponse.getOutboundFlights()).getFlights().forEach(faresInfo -> outboundFlights.put(faresInfo.getJourneySellKey(), faresInfo));
Objects.requireNonNull(flightsResponse.getReturnFlights()).getFlights().forEach(faresInfo -> inboundFlights.put(faresInfo.getJourneySellKey(), faresInfo));
snapDocument.setOutboundFlights(outboundFlights);
snapDocument.setInboundFlights(inboundFlights);
return snapDocument;
}
这是我写的测试用例
public void updateFilghtsSuccess(){
when(snapService.getSnapDocument(any())).thenReturn(snapDocument);
when(snapService.updateFlights(snapDocument, flightListingResponse)).thenReturn(snapDocument);
verify(snapService).updateFlights(snapDocument, flightListingResponse);
}
构建失败,如何为此编写测试用例以提高代码的覆盖率?
【问题讨论】:
-
不清楚你在问什么。这里根本没有测试该方法中的任何内容-您没有验证
updateFlights使用的任何方法是否已被调用等。但是请注意,如果您正在测试updateFlights,您可能想要测试结果@ 987654325@ 而不是updateFlights的内部实现,但是 that 测试的难易程度可能决定了updateFlights最终如何被测试。
标签: java unit-testing testing mockito