PowerMockRunnerを使いながらTheoriesも使いたいときに標準エラーが出力されたので対処メモとして書きます。
・環境
junt 4.10
powermock 1.6.4
mockito 1.10.19
試しに作成したソースは下記のソースです。
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Theories.class)
@PrepareForTest({System.class})
public class PowerMockAndTheories {
@DataPoints
public static TestFixture[] PARAMS = {
new TestFixture("A", "B")
};
@Theory
public void Theoriesを使うよ(TestFixture fixture) {
// SetUp
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getenv(fixture.input)).thenReturn("B");
// Test
String actual = System.getenv(fixture.input);
// Verify
assertThat(actual, is(fixture.expected));
PowerMockito.verifyStatic();
}
static class TestFixture {
final String input;
final String expected;
public TestFixture(String input, String expected) {
this.input = input;
this.expected = expected;
}
}
}
ソースを実行するとIllegalArgumentExceptionの標準エラーが発生します。(テスト自体は成功します)
java.lang.IllegalArgumentException: Unable to determine method-name from description=Theoriesを使うよ(mock.PowerMockAndTheories); - ignored
at org.powermock.modules.junit4.internal.impl.NotificationBuilder.determineTestMethod(NotificationBuilder.java:155)
at org.powermock.modules.junit4.internal.impl.NotificationBuilder.testInstanceCreated(NotificationBuilder.java:265)
at org.powermock.modules.junit4.internal.impl.PowerMockRunNotifier.testInstanceCreated(PowerMockRunNotifier.java:82)
ソースコードを見ているとテストメソッド名が変数に設定されていないようだったので、@Theoryの下に@Testを付けて実行すると標準エラーはでなくなりました。
@Theory
@Test
public void Theoriesを使うよ(TestFixture fixture) {
// SetUp
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getenv(fixture.input)).thenReturn("B");
標準エラーが出たままが気になる方は試してみてください。
(Parameterized.classを使えばいいだけの話かもしれませんが)