社内se × プログラマ × ビッグデータ

プログラミングなどITに興味があります。

Java 例外(Exception) テスト

例外(Exception)のテストの書き方でよく知られているもので、Ruleアノテーションを用いた ExpectedException を使う方法があります。
例外のメッセージまで評価してくれるのがいいですね。

public class RuleTest {
	@Rule
	public ExpectedException expectedException = ExpectedException.none();

	@Test
	public void testThrowException() {
		expectedException.expect(RuntimeException.class);
		expectedException.expectMessage("Rule test");
		throw new RuntimeException("Rule test");
	}
}