2013-10-23 4 views
3

JUnit을 사용하고 있으며 사용자 정의 예외 클래스를 테스트하는 방법을 잘 모르고 있습니다. 나는 창조했다,JUnit 테스트 사용자 정의 예외

public class CustomException extends Exception { 

    //@param message is the exception message 

    public CustomException(final String message) { 
     super(message); 
    } 

    //@param message is the exception message 
    //@param cause is the cause of the original exception 

    public CustomException(final String message, final Throwable cause) { 
     super(message, cause); 

    } 
} 

주류는 많은 것을 시도 할 것입니다;

catch (ParseException e) { 

      throw new CustomException("Date format incorerect", e); 

나는 테스트 클래스를 작성하는 방법을 잘 모르겠다. 도움이 감사하겠습니다.

감사합니다.

+0

무언가를 테스트하는 방법을 알기 위해서는 무엇을해야할 지에 대한 사양이 필요합니다. 이러한 사양을 제공하지 않았으므로이 질문에 답할 수 없습니다. – Raedwald

답변

10

page을 시도 할 수 있습니다. 가장 간단한 경우는 다음과 같이하십시오.

@Test(expected= CustomException.class) 
public void myTest() { 
    MyObject obj = new MyObject(); 
    obj.doSomethingThatMightThrowCustomException(); 
} 
0

나는 이것이 당신을 도울 수 있기를 바랍니다.

public class YourTestClass 
{ 
    @Test 
    public void yourTestMethodName() throws CustomeException { 
     //your logic goes here. 
     if (condition) { 
      throw new CustomeException(<Message to display while throwing an error>); 
     } 
    } 
} 

또한 당신은 당신이 알아야 할 모든 것을 말해야 다음 사이트를 http://junit.sourceforge.net/javadoc/org/junit/Test.html