I want to use PowerMock in a class that combines parameterized tests and ordinary tests

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

What I wanted to do

--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.

I looked it up

--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.

Library configuration

verification code

Test target

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
    }
}

test

@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"));
        }
    }
}

reference

http://tomoyamkung.net/2013/08/28/java-junit4-enclosed/ https://stackoverflow.com/questions/28027445/powermock-access-private-members

Recommended Posts

I want to use PowerMock in a class that combines parameterized tests and ordinary tests
I want to use a little icon in Rails
I want to use swipeback on a screen that uses XLPagerTabStrip
I want to use @Autowired in Servlet
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
[Java Spring MVC] I want to use DI in my own class
I made a class that can use JUMAN and KNP from Java
I want to use arrow notation in Ruby
I want to recursively get the superclass and interface of a certain class
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to define a function in Rails Console
I want to click a GoogleMap pin in RSpec
I want to find a relative path in a situation using Path
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to use FireBase to display a timeline like Twitter
[Ruby] I want to put an array in a variable. I want to convert to an array
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
Which class should I use to get the date and time in my Rails app (Time, DateTime, TimeWithZone)
I tried to make a parent class of a value object in Ruby
[Rails] I want to send data of different models in a form
I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to display a PDF in Chinese (Korean) with thin reports
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
I want to ForEach an array with a Lambda expression in Java
"Teacher, I want to implement a login function in Spring" ① Hello World
I want to develop a web application!
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to easily put CentOS-7 in a PC that I no longer need
[Android] I want to create a ViewPager that can be used for tutorials
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
[Java] How to use FileReader class and BufferedReader class
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
I want to send an email in Java.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
How to create a class that inherits class information
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to design a structured exception handling
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to get the value in Ruby
[Java] How to use Calendar class and Date class
[Ruby] I want to make a program that displays today's day of the week!
I want to issue a connection when a database is created using Spring and MyBatis