2011-03-23 9 views
3

내 자신의 JUnit Assert를 작성하고 있습니까? 어떻게 테스트합니까?유닛은 어떻게 커스텀 어썰트를 테스트합니까?

나는 그것을 전달할 수있는 방법과 실패하게 만드는 방법을 알고 있지만, 그런 것들에 대한 JUnit 테스트는 어떻게 작성합니까?

public static void assertSomething() { 
    if (!something()) { 
     fail("Expected something, but ..."); 
    } 
} 

가 어떻게 그 실패 잡을 수 :

사용자 지정 어설 ​​같이 보일 것인가?

답변

6

fail()junit.framework.AssertionFailedError을 던집니다. 원한다면 어설 션 메서드의 단위 테스트에서 catch 할 수 있습니다.

예 :

@Test(expected = AssertionFailedError.class) 
public void testMyAssertFails() { 
    assertSomething("valueThatWillFail"); 
} 

@Test 
public void testMyAssertPasses() { 
    assertSomething("valueThatPasses"); 
    //if you reach this line, no failure was thrown 
} 
관련 문제