PowerMockAgent

Arthur Zagretdinov edited this page Aug 13, 2017 · 9 revisions

Bootstrapping using a Java agent

   Feature avaliable since PowerMock 1.4.9

Since version 1.4.9 it's possible to bootstrap PowerMock using a Java agent instead of using the PowerMockRunner and the RunWith annotation. This allows you to use e.g. other JUnit runners while still benefiting from PowerMock's functionality. The main difference between the agent based bootstrapper and the classloading based bootstrapper is that you don't run into classloading issues when using XML frameworks etc. It's recommended to use this way of bootstrapping when using PowerMock for integration testing larger parts of a system.

JUnit

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

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

     // Tests goes here
     ...
}

In some cases it may be necessary to manually start the agent before the test is being run. You can do that using:

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

   ..
}

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

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

Maven

JUnit

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4-rule-agent</artifactId>
  <version>1.7.1</version>
  <scope>test</scope>
</dependency>

TestNG

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-testng-agent</artifactId>
  <version>1.7.1</version>
  <scope>test</scope>
</dependency>

Eager loading of the PowerMock Agent in Maven

In some cases (such as mocking final classes) it may be necessary to load the PowerMock agent eagerly in Maven in order for the tests to work in Surefire. If you experience this please 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.localRepository}/org/powermock/powermock-module-javaagent/1.7.1/powermock-module-javaagent-1.7.1.jar
                    </argLine>
                    <useSystemClassloader>true</useSystemClassloader>
                </configuration>
            </plugin>
        </plugins>
</build>  

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

JUnit

Eager loading of the PowerMock Agent with JUnit in Eclipse

To load the PowerMock agent eagerly with Eclipse and JUnit you must first go into the Run Configuration dialog and add the following JVM parameter:

-javaagent: <jarpath>/powermock-module-javaagent-1.7.1.jar

Next must also make sure to put powermock-module-javaagent-1.7.1.jar in the Run Configuration's classpath, before the default classpath (this is to ensure the agent loads before junit).

Current known limitations

  • No way of suppressing static initializers
  • Cannot change value of static final fields

References