2016-10-27 102 views
-2

我的類包含以下handleException方法,我必須使用完整的代碼覆蓋率編寫JUnit。由於我是新來的JUnit任何人可以幫助如何爲例外編寫Junit

public void handleException(Exception exception, DCMRequestDTO requestDTO, DCMResponseDTO responseDTO, String callType) { 
    logger.error("Exception occured for deviceId:" + requestDTO.getDeviceID() + ",RefNum:"+ 
      requestDTO.getRefNum() + ",Operation:" + requestDTO.getOperation() + "::" + exception.getMessage()); 
    exception.printStackTrace(); 
    String errorMessageKey = "GENERIC_EXCEPTION"; 
    if (exception instanceof TooManyInflightException) { 
     errorMessageKey = "THROTTLE_EXCEPTION"; 
    } else if (exception.getCause() instanceof SocketTimeoutException || 
      (exception.getCause() != null && exception.getCause().getCause() instanceof SocketTimeoutException)) { 
     errorMessageKey = "TIMEOUT_EXCEPTION"; 
    } else if (exception.getCause().getCause() instanceof ConnectException || exception.getCause().getCause() instanceof ConnectTimeoutException || exception.getCause().getCause() instanceof UnknownHostException || exception.getCause().getCause() instanceof MalformedURLException || exception.getCause().getCause() instanceof SocketException || (exception.getMessage().indexOf("404 Error") != -1)) { 
     errorMessageKey = "CONNECTION_EXCEPTION"; 
    } else if(exception.getMessage().indexOf("500")!=-1){ 
     errorMessageKey = "INTERNAL_ERROR"; 
    } 
    /* 
    * else if (exception instanceof DCMException) { if (exception.getCause().getCause() instanceof SocketTimeoutException) { errorMessageKey = "TIMEOUT_EXCEPTION"; } else if (exception.getCause().getCause() instanceof ConnectException || exception.getCause().getCause() instanceof IOException || exception.getCause().getCause() instanceof SocketException || (exception.getMessage().indexOf("404 Error") != -1)) { errorMessageKey = "CONNECTION_EXCEPTION"; } } 
    */ 
    // logger.error(errorMessageKey + " occured in DCMServiceImpl :: UniqueId = " + requestDTO.getUniqueId() + ", RefNum = " + requestDTO.getRefNum() + ", Operation = " + requestDTO.getOperation() + ", callType = " + callType, exception); 
    // added for reset time while exception occured 
    responseDTO.setErrorCode(0); 
    responseDTO.setErrorString(smartGraphUtils.getProperty(errorMessageKey)); 
    responseDTO.setStackTrace(exception.toString()); 
    responseDTO.setHasJson(false); 
    requestDTO.setHidResolveGetRetrains("Undetermined"); 
} 

回答

1

根據你的源代碼,你需要測試內周邊類的一個實例:那麼你應該能夠調用的方法handleException。每個測試應該只包含一個斷言(最好的情況下)。這很容易理解您目前正在查看的內容。 下面是一個例子:

public final class MessageBusTest { 
    @Test(expected = NullPointerException.class) 
    public void registerWithNullThrowsNullPointerException() { 
     MessageBus.INSTANCE.register(null); 
    } 
} 

註釋@Test有助於JUnit來找出哪個類中的方法測試。在我的例子中,我調用了一個方法null,並期望拋出異常,因此expected = NullPointerException.class。你的情況你不得不如準備適當的Exception進行測試,然後使用assertEquals檢查requestDTO的內容(即錯誤字符串)。

0

首先,您的方法有很多流程,您需要爲方法中的每個流程/條件編寫測試以確保處理所有方案。

示例代碼,請參見下文下手,你可以寫完整的邏輯:

import org.junit.Test; 

    public class HandleExceptionText { 

     @Test 
     public void testHandleExceptionForTooManyInflightException() { 
      //throw TooManyInflightException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new TooManyInflightException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForSocketTimeoutException() { 
      //throw SocketTimeoutException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new SocketTimeoutException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForConnectTimeoutException() { 
      //throw ConnectTimeoutException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new ConnectTimeoutException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForConnectException() { 
      //throw ConnectException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new ConnectException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForUnknownHostException() { 
      //throw UnknownHostException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new UnknownHostException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForMalformedURLException() { 
      //throw MalformedURLException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new MalformedURLException()); 
      //Assert to check responseDTO values have been set 
     } 

     @Test 
     public void testHandleExceptionForConnectException() { 
      //throw SocketException exception and then call handleException() 
      //Example using Mockito: when(obj.method(parms)).thenThrow(new SocketException()); 
      //Assert to check responseDTO values have been set 
     } 

    } 

另外,我建議去通過下面的JUnit &的Mockito教程鏈接: https://dzone.com/articles/junit-tutorial-beginners https://www.tutorialspoint.com/mockito/mockito_exception_handling.htm

+0

@測試 \t public final void testhandleException()throws Exception {given(smartGraphServiceHelper.generateDocument(Mockito.any(DCMRequestDTO.class)))。willThrow(new SocketTimeoutException()); \t \t \t \t String callType =「」; \t \t \t \t smartGraphService.handleException(exception,requestDTO,responseDTO,callType); \t \t assertEquals(「0」,responseDTO.getErrorCode()); \t}這是我嘗試過的,但當它進入exception.getCause() – Amit