문제

Nunit의 expectexception 또는 assert.throws <> junit에 해당합니까?

도움이 되었습니까?

해결책

Junit4 :

@Test(expected = org.dom4j.DocumentException.class)
void shouldThrowException() {
    getFile(null);
}

Junit3 :

void testShouldThrowException() {
    try {
      getFile(null);
      fail("Expected Exception DocumentException");
    } catch(DocumentException e) {}
}

다른 팁

더 풍부한 예외 일치를 제공하는 Expectexception 클래스를 살펴 보는 것을 고려할 수도 있습니다.

https://github.com/junit-team/junit/wiki/exception-testing

예외 클래스와 일치 할 수있을뿐만 아니라 메시지에 사용자 정의 매칭을 적용 할 수 있습니다.

Junit 테스트에 Groovy를 사용하는 경우 사용할 수 있습니다. 야생.

junit3 스타일을 사용하는 예는 다음과 같습니다.

void testShouldThrowException() {
    def message = shouldFail(DocumentException) {
        documentService.getFile(null)
    }
    assert message == 'Document could not be saved because it ate the homework.'
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top