One: use @PowerMockRunnerDelegate

Features available from PowerMock 1.6.0.Copy the code

Since PowerMock 1.6.0, it is supported to delegate test execution to another JUnit Runner instead of using the JUnit Rule. This leaves the actual test execution to another runner of your choice. For example, tests can be delegated to a “SpringJunit4ClassRunner,” “Parameterized,” or “Enclosed” runner. Usage Examples:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
@PrepareForTest({FinalDemo.class, PrivateFinal.class})
public class FinalDemoTest {

    @Parameterized.Parameter(0)
    public String expected;

    @Parameterized.Parameters(name = "expected={0}")
    public staticCollection<? > expections() {return java.util.Arrays.asList(new Object[][]{
            {"Hello altered World"}, {"something"}, {"test"}}); }@Test
    public void assertMockFinalWithExpectationsWorks(a) throws Exception {
        final String argument = "hello";

        FinalDemo tested = mock(FinalDemo.class);

        when(tested.say(argument)).thenReturn(expected);

        final String actual = "" + tested.say(argument);

        verify(tested).say(argument);

        assertEquals("Expected and actual did not match", expected, actual); }}Copy the code

reference

  • Jayway Blog: Using another JUnit runner with PowerMock

Two: use JUnit Rule to boot the program

Functionality is available from PowerMock 1.4.Copy the code

Starting with version 1.4, you can boot PowerMock using JUnit Rules instead of PowerMockRunner and RunWith annotations. This allows you to run programs with other JUnit while still benefiting from PowerMock’s capabilities. You can specify this by:

@PrepareForTest(X.class);
public class MyTest {
    @Rule
    PowerMockRule rule = new PowerMockRule();

    // Tests goes here. }Copy the code

Use PowerMockRule with Maven

You need to rely on these items:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4-rule</artifactId>
  <version>2.0.2</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-classloading-xstream</artifactId>
  <version>2.0.2</version>
  <scope>test</scope>
</dependency>
Copy the code

You can also replace powermock-classloading-xstream with the Objenesis version:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-classloading-objenesis</artifactId>
  <version>2.0.2</version>
  <scope>test</scope>
</dependency>
Copy the code

However, this version is not as stable as the XStream version, but it does not require any additional dependencies.

Warning: Use version PowerMock 1.4 (or later), because prior to this version, the Rule didn’t actually perform any test methods (but it looks that way).

Do not use PowerMockRule with Maven

You’ll need to download Powermock-module-junit4-rule, powermock-classloading-base, and One of powermock-classloading-xstream or powermock-classloading-objenesis and put it in your classpath.

reference

  • Mockito sample
  • Spring integration testing using PowerMock and Mockito examples.

Three: Boot with a Java proxy

Boot with a Java proxy

Functionality is available from PowerMock 1.4.9Copy the code

Starting with version 1.4.9, PowerMock can be bootstrapped using Java proxies rather than PowerMockRunner and RunWith annotations. This allows you to run programs using other junits, for example, while still benefiting from PowerMock’s capabilities. The main difference between proxy-based and classload-based bootstrap is that you don’t encounter classloading problems when using XML frameworks, etc. It is recommended to use this boot approach when you are integrating larger portions of your system with PowerMock.

JUnit

To bootstrap the Agent in JUnit you can use the PowerMockRule in the powermock-module-junit4-rule-agent project. For example:

To boot the agent in JUnit, you can use PowerMockRule in the Powermock-module-junit4-rule-Agent project. Such as:

@PrepareForTest(X.class);
public class MyTest {
     @Rule
     PowerMockRule rule = new PowerMockRule();

     // Tests goes here. }Copy the code

In some cases, you may need to start the agent manually before running tests. You can use:

public class MyTest {
   static{ PowerMockAgent.initializeIfNeeded(); }.. }Copy the code

It’s recommended that you put powermock-module-junit4-rule-agent before junit in the classpath.

It is recommended that you place powermock-module-junit4-rule-agent in the classpath before JUnit.

TestNG

To bootstrap the Agent in TestNG you should extend from PowerMockTestCase in the powermock-module-testng-common project and you need to have the jar file from powermock-module-testng-agent in classpath. For example:

@PrepareForTest(X.class)
public class SomeTest extends PowerMockTestCase {... }Copy the code

Maven

JUnit

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4-rule-agent</artifactId>
  <version>2.0.2</version>
  <scope>test</scope>
</dependency>
Copy the code

TestNG

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-testng-agent</artifactId>
  <version>2.0.2</version>
  <scope>test</scope>
</dependency>
Copy the code

Preload the PowerMock proxy in Maven

In some cases, such as the Mock final class, you may need to urgently load the PowerMock agent in Maven in order to test it to work in Surefire. If this happens to you, add the following to your POM.xml:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <argLine>- javaagent: ${Settings. The localRepository} / org/powermock/powermock - module - javaagent / 2.0.2 / powermock - module - javaagent - 2.0.2. J ar</argLine>
                    <useSystemClassloader>true</useSystemClassloader>
                </configuration>
            </plugin>
        </plugins>
</build>  
Copy the code

The Maven users

You need to download powermock-java-agent (source, javadoc) and either powermock-module-junit4-rule-agent (sources, javadoc) if using JUnit or powermock-module-testng-agent (sources, javadoc) if using TestNG.

You’ll need to download Powermock-Java-Agent (source, Javadoc); Powermock-module-junit4-rule-agent (sources, Javadoc) You also need powermock-module-testng-agent (sources, javadoc).

JUnit

Preload the PowerMock proxy in Eclipse using JUnit

To eagerly load the PowerMock proxy using Eclipse and JUnit, you must first go to the Run Configuration dialog box and add the following JVM parameters:

- javaagent: < jarpath > / powermock - module - javaagent - 2.0.2. JarCopy the code

Next, you must also ensure that powermock-module-JavaAgent-2.0.2.jar is placed in the classpath of the running configuration, before the default classpath (this is to ensure that the proxy is loaded before junit).

Current known limitations

  • There is no suppression of statically initializing code blocks
  • Cannot change the value of a static final field

reference

  • JUnit example
  • Spring integration testing using PowerMock and Mockito examples.