私は現在、JUnit テストでつまずいており、助けが必要です。したがって、いくつかのオブジェクトをリファクタリングする静的メソッドを備えたこのクラスを取得しました。簡単にするために、小さな例を作成しました。これは私のファクトリークラスです:
class Factory {
public static String factorObject() throws Exception {
String s = "Hello Mary Lou";
checkString(s);
return s;
}
private static void checkString(String s) throws Exception {
throw new Exception();
}
}
そして、これは私のテストクラスです:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Factory.class })
public class Tests extends TestCase {
public void testFactory() throws Exception {
mockStatic(Factory.class);
suppress(method(Factory.class, "checkString"));
String s = Factory.factorObject();
assertEquals("Hello Mary Lou", s);
}
}
基本的に私が達成しようとしたのは、プライベートメソッド checkString() を抑制し (例外がスローされないようにする)、メソッド checkString() がメソッド factorObject() で実際に呼び出されたことを確認する必要があることです。
更新: 抑制は、次のコードで正しく機能します。
suppress(method(Factory.class, "checkString", String.class));
String s = Factory.factorObject();
...しかし、文字列「s」に対してNULLを返します。何故ですか?