2016-08-25 219 views
0

我正在爲一種方法編寫JUnit測試用例。我已經爲它寫了積極的情景,但我不確定該方法是正確編寫還是設計的,因爲我無法進入Catch塊來捕獲異常。我需要這個更好的分支機構。我無法使用Mockito,因爲DaoExceptionChecked ExceptionJava中的捕捉異常

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

任何幫助,將不勝感激

感謝,

+0

你是什麼意思,我不能使用Mockito,因爲DaoException是一個檢查異常?你應該能夠讓你的模擬拋出任何異常... – Morfic

+0

因爲Mockito只拋出RunTimeException。 – Jaykumar

+0

我可以嘲笑它,但它仍然不在Catch塊中。 Mockito.when(integrationManager.getUiIntegrationDao()。retrieveUiIntegrationReportData(agencyCode,createdDays,transactionStatus,transactionStatus,accounts))。ThenThrow(DaoException.class); – Jaykumar

回答

0

也許你可以把事物的PowerMocking的一面, 「工作」在那個靜態的通話中。

我這裏常用的建議是:如果有一些靜態調用令人討厭你;然後擺脫它。

只需創建一個界面,表示您需要的功能;然後創建一個執行靜態調用的簡單實現(在生產環境中使用)。在您的測試環境中,您使用依賴注入。含義:你創建了一個模擬的該新界面的實例;然後你把它交給你的班級。

就是這樣。突然你只需要EasyMock,來嘲笑一個普通的花園非靜態方法調用。

猜猜看:這也改善了你的設計;因爲它允許你從包含該靜態方法的類中完全分離客戶端代碼!