2016-10-27 5 views
-2

My Classには、完全なコードカバレッジでJUnitを書く必要があるhandleExceptionメソッドが含まれています。私はあなたのソースコードによると、誰もが例外のために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

を助けることができるのJUnitに新しい午前として、あなたは、テストの内部周囲のクラスのインスタンスを必要とする:あなたはメソッドhandleExceptionを呼び出すことができるはずです。 各テストは、最良の場合には1つのアサートだけで構成する必要があります。これはあなたが現在チェックしていることを理解しやすくします。ここで は一例です:

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

注釈@Testはテストですクラスのどのメソッドを把握するJUnitのに役立ちます。私の例では、nullというメソッドを呼び出し、例外がスローされると予想します。したがって、expected = NullPointerException.classです。

あなたの場合は、たとえばテストのために適切なExceptionを準備してから、コンテンツ(エラー文字列)のにassertEqualsを付けて確認します。

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()の内部に入るときにnullpointerを与えます。 – Amit

関連する問題