Ich schreibe einen JUnit Testfall für eine Methode. Ich habe das positive Szenario dafür geschrieben, aber ich bin mir nicht sicher, ob die Methode korrekt geschrieben oder entworfen wurde, weil ich nicht in den Catch
Block gehen kann, um die Ausnahme zu fangen. Ich brauche das für eine bessere Filialabdeckung. Ich kann Mockito
nicht verwenden, weil DaoException
ein Checked Exception
ist.Catching Exception in Java
MUT
public List<IUiIntegrationDto> retrieveUiIntegrationReportData(List<String> agencyCode, Integer createdDays, String lob, String transactionStatus,
List<String> accounts, String sortKey, String sortOrder) throws ManagerException {
List<IUiIntegrationDto> intgList = new ArrayList<IUiIntegrationDto>();
try {
intgList = getUiIntegrationDao().retrieveUiIntegrationReportData(agencyCode, createdDays, lob, transactionStatus, accounts);
if (null != intgList) {
ComparatorUtil.sortESignatureIntegrationFields(intgList, sortKey, sortOrder);
}
} catch (DaoException de) {
String message = "Error retrieving ui integration report data";
IExceptionHandlerResponse r = getExceptionHandler().handleData(de, ManagerException.class, message);
if (r.isRethrow()) {
ManagerException me = (ManagerException) r.getThrowable();
throw me;
}
}
return intgList;
}
JUnit
@Test(expected = DaoException.class)
public void testRetrieveUiIntegrationReportData_Exception() throws Exception {
List<String> agencyCode = new ArrayList<>();
agencyCode.add("044494");
agencyCode.add("044400");
Integer createdDays = 30;
String lob = "01";
String transactionStatus = "Completed";
List<String> accounts = new ArrayList<>();
accounts.add("CorpESignClientUser");
accounts.add("GW_SYS_USER");
String sortKey = "createdDate";
String sortOrder = "desc";
UiIntegrationManager integrationManager = new UiIntegrationManager();
IUiIntegrationDao integrationDao = Mockito.mock(IUiIntegrationDao.class);
IUiIntegrationDto uiIntegrationDto = new UiIntegrationDto();
uiIntegrationDto.setClientUser("CorpESignClientUser");
List<IUiIntegrationDto> integrationDto = new ArrayList<>();
integrationDto.add(uiIntegrationDto);
integrationManager.setUiIntegrationDao(integrationDao);
// Mockito.doThrow(new DaoException("Exception thrown")).when(integrationDao.retrieveUiIntegrationReportData(agencyCode, createdDays, lob, transactionStatus, accounts));
// Mockito.when(integrationDao.retrieveUiIntegrationReportData(agencyCode, createdDays, lob, transactionStatus, accounts)).thenReturn(integrationDto);
integrationDto = integrationManager.retrieveUiIntegrationReportData(agencyCode, createdDays, lob, transactionStatus, accounts, sortKey, sortOrder);
assertNotNull(integrationDto);
assertFalse(agencyCode.isEmpty());
assertEquals(2, agencyCode.size());
assertNotNull(accounts);
assertEquals("CorpESignClientUser", accounts.get(0));
assertFalse(integrationDto instanceof ArrayList);
}
Jede mögliche Hilfe
Danke,
Was genau meinst du mit 'Ich kann Mockito nicht verwenden, da DaoException eine Checked Exception ist'? Sie sollten in der Lage sein, Ihre mock jede Ausnahme auszulösen ... – Morfic
Weil Mockito nur RunTimeException auslöst. – Jaykumar
Ich kann es verspotten, aber es geht immer noch nicht in den Catch-Block. Mockito.when (integrationManager.getUiIntegrationDao(). RetrieveUiIntegrationReportData (agencyCode, createdDays, transactionStatus, transactionStatus, accounts)). ThenThrow (DaoException.class); – Jaykumar