メソッドが何もしないかどうかをテストする方法を教えてください。たとえば、私は、指定された文字列引数がnullまたは空の場合(それは引数の検証のためのものです)、例外をスローする静的メソッドを持っています。今、私のテストでは、次のようになります。JUnit4 - メソッドが何もしないかどうかをテストします。
@Test
public void notNullOrEmpty_doesNothingIfValueIsNotNullOrEmpty() {
Require.notNullOrEmpty(Generate.randomString());
assertTrue(true); // <- this looks very ugly
}
@Test(expected = IllegalArgumentException.class)
public void notNullOrEmpty_throwsExceptionIfValueIsNull() {
Require.notNullOrEmpty(null);
}
@Test(expected = IllegalArgumentException.class)
public void notNullOrEmpty_throwsExceptionIfValueIsEmpty() {
Require.notNullOrEmpty("");
}
私はassertTrue(true)
を呼び出すことなく通過する最初のテストをすることができ、Assert.fail()
があるどのようにAssert.pass()
のようなものはありますか?
EDIT: はあなたが@Test(expected = YourException.class)
注釈を追加する必要があり、第3の試験に
本当にただグアバライブラリPreconditions.checkArgument(Strings.isNullOrEmpty(「のmyString」))から十分にテストクラスを使用することを検討してnullまたは空かどうかを確認ありません。 – sandrozbinden
ランダムに生成された文字列をテスト入力として慎重に使用することも忘れないでください。 http://stackoverflow.com/questions/3441686/what-are-the-downsides-using-random-values-in-unit-testing – sandrozbinden