public class Foo {
public void shutdown() {
System.exit(0);
}
}
Fooクラスのshutdown()メソッドで、以下の挙動をassertする!!
テストケース:
@RunWith(PowerMockRunner.class) @PrepareForTest( { Foo.class }) public class FooTest extends TestCase { @Test public void shutdown() { PowerMockito.mockStatic(System.class); Foo f = new Foo(); f.shutdown(); PowerMockito.verifyStatic(Mockito.times(1)); System.exit(0); } }
これでOK。
色々試行錯誤したのだけど、"void"メソッドだからと
PowerMockito.doNothing().when(System.class);
してみたが、これ入れると以下のような例外が出てしまい、手詰まりに。
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:31)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. although stubbed methods may return mocks, you cannot inline mock creation (mock()) call
inside a thenReturn method (see issue 53)
外したら動いた。この辺が曖昧なのが多少気持ち悪いが、とりあえず前進を再開できる。
参考メモ:
Hotな話題だけに、トピック記事もsampleも出揃いつつあるのだけれど、中々"Spot On!"な回答/sampleが無くてやきもきした。