TL;DR
--Only @Runwith (Enclosed.class) for external classes
--I will add the following to the inner class
- @RunWith(PowerMockRunner.class)
--@PrepareForTest ({tested class})
- @PowerMockRunnerDelegate(Parameterized.class)
--@PowerMockRunnerDelegate (JUnit4.class) is not required
--static initializer I wanted to write a test of the processing to be executed --There was a mixture of parameterized tests and tests that you wanted to run only once. ――However, it is unpleasant to distribute the test code for one class in multiple places.
--Tests that interrupt static can be done with Mockito + PowerMock
--Parameterized tests can be done using @RunWith (Theories.class) or @RunWith (Parameterized.class)
--If it is Parameterized, it asserts for each parameter, so I adopted this one.
--You can use @ RunWith (Enclosed.class) to combine multiple test classes into one class.
public class StaticClass {
    public static String MESSAGE;
    static {
        int i = new Random().nextInt();
        if (0 <= i && i <= 10) {
            MESSAGE = "Parameter is inside of range, 0 to 10.";
        } else {
            MESSAGE = "Nmm...?";
            method();
        }
    }
    private static void method() {
        // to do any action
    }
}
@RunWith(Enclosed.class)
public class StaticClassTest {
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({StaticClass.class})
    @PowerMockRunnerDelegate(Parameterized.class)
    public static class ParameterizeTest {
        @Parameterized.Parameter
        public Integer parameter;
        @Parameterized.Parameters(name = "parameter is {0}")
        public static Object[][] parameterSupplier() {
            return new Object[][]{
                    {0},
                    {1},
                    {2},
                    {3},
                    {4},
                    {5},
                    {6},
                    {7},
                    {8},
                    {9},
                    {10}
            };
        }
        @Test
        public void test() throws Exception {
            // given
            Random random = PowerMockito.mock(Random.class);
            PowerMockito.whenNew(Random.class).withNoArguments().thenReturn(random);
            PowerMockito.when(random.nextInt()).thenReturn(parameter);
            // expect
            assertEquals("Parameter is inside of range, 0 to 10.",
                    Whitebox.getInternalState(StaticClass.class, "MESSAGE"));
        }
    }
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({StaticClass.class})
    public static class NormalTest {
        @Test
        public void test() throws Exception {
            // given
            Random random = PowerMockito.mock(Random.class);
            PowerMockito.whenNew(Random.class).withNoArguments().thenReturn(random);
            PowerMockito.when(random.nextInt()).thenReturn(99);
            // expect
            assertEquals("Nmm...?",
                    Whitebox.getInternalState(StaticClass.class, "MESSAGE"));
        }
    }
}
http://tomoyamkung.net/2013/08/28/java-junit4-enclosed/ https://stackoverflow.com/questions/28027445/powermock-access-private-members
Recommended Posts