8

jUnitにNUnitのExpectedExceptionまたはAssert.Throws<>に相当するものはありますか?

4

3 に答える 3

11

より豊富な例外マッチングを提供するExpectedExceptionクラスを確認することも検討してください。

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

例外クラスを照合できるだけでなく、そのメッセージにカスタムマッチャーを適用することもできます。

于 2010-11-24T09:53:16.960 に答える
7

junit4:

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

junit3:

void testShouldThrowException() {
    try {
      getFile(null);
      fail("Expected Exception DocumentException");
    } catch(DocumentException e) {}
}
于 2009-06-27T11:40:32.537 に答える
2

junitテストにGroovyを使用している場合は、shouldFailを使用できます。

junit3スタイルを使用した例を次に示します。

void testShouldThrowException() {
    def message = shouldFail(DocumentException) {
        documentService.getFile(null)
    }
    assert message == 'Document could not be saved because it ate the homework.'
}
于 2012-03-29T23:24:22.200 に答える