-
Notifications
You must be signed in to change notification settings - Fork 585
PowerMockAgent
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.
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.
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 {
...
}
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng-agent</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
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/2.0.2/powermock-module-javaagent-2.0.2.jar
</argLine>
<useSystemClassloader>true</useSystemClassloader>
</configuration>
</plugin>
</plugins>
</build>
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.
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-2.0.2.jar
Next you must also make sure to put powermock-module-javaagent-2.0.2.jar in the Run Configuration's classpath, before the default classpath (this is to ensure the agent loads before junit).
- No way of suppressing static initializers
- Cannot change value of static final fields
- JUnit Examples
- Spring Integration Test with PowerMock and Mockito example.