これは、静的メソッドの構文が正しくない場合にdoThrow...を使用しているためです。これにアプローチする方法はいくつかありますが、以下の2つの別々のテスト方法で概説しました。
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticService.class})
public class StaticServiceTest {
@Test (expected = IllegalArgumentException.class)
public void testMockStatic1() throws Exception {
String sayString = "hello";
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class, "say", Matchers.eq(sayString));
StaticService.say(sayString);
fail("Expected exception not thrown.");
}
@Test (expected = IllegalArgumentException.class)
public void testMockStatic2() throws Exception {
String sayString = "hello";
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(
StaticService.class);
StaticService.say(Matchers.eq(sayString)); //Using the Matchers.eq() is
//optional in this case.
//Do NOT use verifyStatic() here. The method hasn't actually been
//invoked yet; you've only established the mock behavior. You shouldn't
//need to "verify" in this case anyway.
StaticService.say(sayString);
fail("Expected exception not thrown.");
}
}
参考までに、これが私が作成したStaticServiceの実装です。それがあなたのものと一致するかどうかはわかりませんが、テストに合格したことを確認しました。
public class StaticService {
public static void say(String arg) {
System.out.println("Say: " + arg);
}
}
関連項目