My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
PowerMockAgent  
Updated Apr 5, 2012 by johan.ha...@gmail.com

Bootstrapping using a Java agent (experimental)

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>${powermock.version}</version>
	<scope>test</scope>
</dependency>

TestNG

<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-module-testng-agent</artifactId>
	<version>${powermock.version}</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.4.12/powermock-module-javaagent-1.4.12.jar
                    </argLine>
                    <useSystemClassloader>true</useSystemClassloader>
                </configuration>
            </plugin>
        </plugins>
</build>  

You can have a look at a working example here.

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

Next must also make sure to put powermock-module-javaagent-1.4.12.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

Refer to the subversion examples for JUnit or TestNG.

Or check out the Spring Integration Test with PowerMock and Mockito example.

Comment by titanium...@gmail.com, Jan 23, 2012

I'm unable to use breakpoints in IntelliJ when using this method (as opposed to using the RunWith? and PowerMockIgnore? annotations), not sure if that's a problem with my setup or a known issue

Comment by omtan...@gmail.com, Mar 27, 2012

I'm also unable to use breakpoints when using PowerMockRule with IntelliJ. Any solution to this issue?

Comment by project member johan.ha...@gmail.com, May 21 (4 days ago)

If you find any solution or hints on how to solve this please keep us posted.


Sign in to add a comment
Powered by Google Project Hosting