我在使用弹簧单元测试和声纳皮棉时遇到了麻烦,因为我具有多个 catch 块,这些块虽然可以通过我的测试,但是没有被覆盖。
这是我的示例代码:
@Autowired
private CompanyServices companyServices;
@Autowired
private InventoryControlRepositoryCustom inventoryControlRepositoryCustom;
@Autowired
private SalesRepositoryCustom salesRepositoryCustom;
@Autowired
private InventoryControlRepository inventoryControlRepository;
public ResponseLinearGraph getLinearGraphInfo(Date startDate, Date endDate) throws DashboardException, CompanyException, InventoryControlException, SaleException{
try {
-->HERE I CATCH COMPANY EXCEPTION IF THERE'S ANY ON COMPANY SERVICE<--
CompanyInfo company = companyServices.getCompanies().get(0);
-->HERE I CATCH INVENTORY CONTROL EXCEPTION IF THERE'S ANY ON INVENTORY CONTROL REPOSITORY<--
List<InventoryControl> invControlList = inventoryControlRepository.getSomething(startDate, endDate);
-->HERE I CATCH SALE EXCEPTION IF THERE'S ANY ON SALE REPOSITORY<--
BigDecimal dailyInputSum = salesRepositoryCustom.doSomething();
-->HERE I CATCH GENERAL EXCEPTION IF ANY AND THROW IT AS A DASHBOARD EXCEPTION<--
//DO SOME OTHER THINGS
return response;
}catch(CompanyException getLinearGraphCompanyEx) {
throw getLinearGraphCompanyEx;
}catch(InventoryControlException getLinearGraphInventoryControlEx) {
throw getLinearGraphInventoryControlEx;
}catch(SaleException getLinearGraphSaleEx) {
throw getLinearGraphSaleEx;
}catch(Exception getLinearGraphInfoGenEx) {
DashboardException getLinearGraphInfoEx = new DashboardException(getLinearGraphInfoGenEx, DashboardException.LAYER_SERVICE, DashboardException.ACTION_LISTS);
log.error(String.format("%s - %s - %s - Error in CONTROLLER LAYER - Get Linear Graph Info - CODE: %s",
LAYER_SERVICE,MODULE_DASHBOARD,ACTION_GET,getLinearGraphInfoEx.getExceptionCode()));
throw getLinearGraphInfoEx;
}
}
这是我的测试:
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void getLinearGraphInfo_throwDashboardException() throws DashboardException, CompanyException, InventoryControlException, SaleException, ParseException{
thrown.expect(DashboardException.class);
Date startDate = TimeUtils.parseDate("2019-07-01 00:00:00", TimeUtils.LONG_DATE);
Date endDate = TimeUtils.parseDate("2019-07-16 23:59:59", TimeUtils.LONG_DATE);
DashboardServices dashboardMock = mock(DashboardServices.class);
when(dashboardMock.getLinearGraphInfo(startDate, endDate)).thenThrow(DashboardException.class);
dashboardMock.getLinearGraphInfo(startDate, endDate);
}
@Test
public void getLinearGraphInfo_throwCompanyException() throws DashboardException, CompanyException, InventoryControlException, SaleException, ParseException{
thrown.expect(CompanyException.class);
Date startDate = TimeUtils.parseDate("2019-07-01 00:00:00", TimeUtils.LONG_DATE);
Date endDate = TimeUtils.parseDate("2019-07-16 23:59:59", TimeUtils.LONG_DATE);
DashboardServices dashboardMock = mock(DashboardServices.class);
when(dashboardMock.getLinearGraphInfo(startDate, endDate)).thenThrow(CompanyException.class);
dashboardMock.getLinearGraphInfo(startDate, endDate);
}
@Test
public void getLinearGraphInfo_throwInvControlException() throws DashboardException, CompanyException, InventoryControlException, SaleException, ParseException{
thrown.expect(InventoryControlException.class);
Date startDate = TimeUtils.parseDate("2019-07-01 00:00:00", TimeUtils.LONG_DATE);
Date endDate = TimeUtils.parseDate("2019-07-16 23:59:59", TimeUtils.LONG_DATE);
DashboardServices dashboardMock = mock(DashboardServices.class);
when(dashboardMock.getLinearGraphInfo(startDate, endDate)).thenThrow(InventoryControlException.class);
dashboardMock.getLinearGraphInfo(startDate, endDate);
}
@Test
public void getLinearGraphInfo_throwSaleException() throws DashboardException, CompanyException, InventoryControlException, SaleException, ParseException{
thrown.expect(SaleException.class);
Date startDate = TimeUtils.parseDate("2019-07-01 00:00:00", TimeUtils.LONG_DATE);
Date endDate = TimeUtils.parseDate("2019-07-16 23:59:59", TimeUtils.LONG_DATE);
DashboardServices dashboardMock = mock(DashboardServices.class);
when(dashboardMock.getLinearGraphInfo(startDate, endDate)).thenThrow(SaleException.class);
dashboardMock.getLinearGraphInfo(startDate, endDate);
}
有什么想法吗?