2014-09-08 6 views
0

junit 또는 testng에서 사용할 수있는 라이브러리 함수가 있습니다. 정규 표현식과 같이 두 문자열을 비교할 수 있습니다. 나는 다양한 예외를 던질 수있는 함수를 가지고 있는데, 나는 하나의 간단한 테스트 케이스에서 모든 메시지를 처리하는 방법을 알아 내려고하고있다.Assert 문에서 정규 표현식 비교

+0

'Exception'의 하위 클래스와 같은 것을 의미합니까? – abiessu

+0

이 방법을 테스트하는 것이 확실합니까? "다양한 예외"가 발생할 수있는 경우 여러 개의 테스트 케이스가있는 것처럼 들립니다. –

+0

아니요 하위 클래스 예외가 아닙니다. – Manipal

답변

2

어때 대략 ExpectedException#expectMessage?

@Rule ExpectedException expectedException = ExpectedException.none(); 

@Test public void yourTest() { 
    // One overload takes a string to test "contains". 
    expectedException.expectMessage("foo"); 

    // The other takes a Hamcrest matcher. startsWith is a Hamcrest core matcher. 
    expectedException.expectMessage(startsWith("Bar")); 

    // If you include the latest Hamcrest source, you can use the brand-new 
    // MatchesPattern matcher, which is the regex solution you're looking for. 
    expectedException.expectMessage(new MatchesPattern(
     new Pattern("Baz.+\[[0-9]+\]"))); 

    systemUnderTest.doExceptionThrowingCode(); 
} 

MatchesPattern에 대한 StartsWith에 대한 문서, 또는 새로운 코드 (십칠일 세를!)를 참조하십시오.