2016-08-25 153 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,来嘲笑一个普通的花园非静态方法调用。

猜猜看:这也改善了你的设计;因为它允许你从包含该静态方法的类中完全分离客户端代码!