jUnitでExpectedException?
-
20-08-2019 - |
質問
jUnit には NUnit の ExpectedException または Assert.Throws<> に相当するものはありますか?
解決
junit4ます:
@Test(expected = org.dom4j.DocumentException.class)
void shouldThrowException() {
getFile(null);
}
junit3ます:
void testShouldThrowException() {
try {
getFile(null);
fail("Expected Exception DocumentException");
} catch(DocumentException e) {}
}
他のヒント
また、より豊かな例外マッチングを提供ExpectedExceptionクラスを見てみ考えるかもしれません。
https://github.com/junit-team/junit/wiki /例外検査する
だけでなく、あなたは、例外クラスを一致させることができますが、また、あなたはそのメッセージにカスタムマッチャを適用することができます。
junit テストに Groovy を使用している場合は、次のように使用できます。 失敗するはずです.
以下は junit3 スタイルを使用した例です。
void testShouldThrowException() {
def message = shouldFail(DocumentException) {
documentService.getFile(null)
}
assert message == 'Document could not be saved because it ate the homework.'
}
所属していません StackOverflow