public class Foo { public void shutdown() { System.exit(0); } } Fooクラスのshutdown()メソッドで、以下の挙動をassertする!! + System.exit()が呼ばれていること。 + その引数に"0"が渡されていること。 テストケース: #code|java|> @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); してみたが、これ入れると以下のような例外が出てしまい、手詰まりに。 #pre||> 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) ||< 外したら動いた。この辺が曖昧なのが多少気持ち悪いが、とりあえず前進を再開できる。 参考メモ: - MockSystem - powermock - Project Hosting on Google Code -- http://code.google.com/p/powermock/wiki/MockSystem - MockitoUsage13 - powermock - Project Hosting on Google Code -- http://code.google.com/p/powermock/wiki/MockitoUsage13 - MockStaticTest.java - powermock - Project Hosting on Google Code -- http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermockito/junit4/src/test/java/samples/powermockito/junit4/staticmocking/MockStaticTest.java?r=1366 - SystemClassUserTest.java - powermock - Project Hosting on Google Code -- http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermockito/junit4/src/test/java/samples/powermockito/junit4/system/SystemClassUserTest.java?r=1366 - StaticPartialMockingTest.java - powermock - Project Hosting on Google Code -- http://code.google.com/p/powermock/source/browse/trunk/modules/module-test/powermockito/junit4/src/test/java/samples/powermockito/junit4/partialmocking/StaticPartialMockingTest.java?r=1366 - mocking void methods with arguments - PowerMock | Google グループ -- http://groups.google.com/group/powermock/browse_thread/thread/4d2462bbe69912e6 - Use Mockito to mock static void method - PowerMock | Google グループ -- http://groups.google.com/group/powermock/browse_thread/thread/e0a3b047518aaf4a/316c87413ac312e0 - Expectations on static void methods? - PowerMock | Google グループ -- http://groups.google.com/group/powermock/browse_thread/thread/383cc9b34b286210/71599db839c14202 Hotな話題だけに、トピック記事もsampleも出揃いつつあるのだけれど、中々"Spot On!"な回答/sampleが無くてやきもきした。