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

Bootstrapping using a JUnit Rule

Since version 1.4 it's possible to bootstrap PowerMock using a JUnit Rule instead of using the PowerMockRunner and the RunWith annotation. This allows you to use other JUnit runners while still benefiting from PowerMock's functionality. You do this by specifying:

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

        // Tests goes here
        ...
   }

Using PowerMockRule with Maven

You need to depend on these projects:

<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-module-junit4-rule</artifactId>
	<version>${powermock.version}</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-classloading-xstream</artifactId>
	<version>${powermock.version}</version>
	<scope>test</scope>
</dependency>

where ${powermock.version} is the version of PowerMock you're currently using. You can also replace powermock-classloading-xstream with an Objenesis version:

<dependency>
	<groupId>org.powermock</groupId>
	<artifactId>powermock-classloading-objenesis</artifactId>
	<version>${powermock.version}</version>
	<scope>test</scope>
</dependency>

How ever this version is not as stable as the xstream version but it doesn't require any additional dependencies.
Warning: Use version PowerMock 1.4 (or later) since prior to this version the rule didn't actually execute any test methods (but it looked like it did).

Using PowerMockRule without Maven

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

References

Refer to the examples in subversion.

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

Comment by ramsunda...@gmail.com, Aug 18, 2011

the artifact is not found in maven repo. please publish it. this seems a really useful library.

Comment by basil...@gmail.com, Oct 28, 2011

In 1.4.10 it does not work.. with xstream it throws com.thoughtworks.xstream.converters.ConversionException?, with objenesis it throws NPE

Comment by vsha...@gmail.com, Oct 31, 2011

I am having the similar problems. Unable to integrate 1.4.10.

Comment by ink.xing...@gmail.com, Nov 11, 2011

I have this issue, too. I got exactlly the same error as: com.thoughtworks.xstream.converters.ConversionException?

What is the stable version for this feature?

Comment by project member johan.ha...@gmail.com, Nov 14, 2011

There is no stable version for this feature. You may need to use @PowerMockIgnore? to defer the loading of the classes that causes the exception to the system classloader.

Comment by enricj...@yahoo.es, Dec 13, 2011

I am unable to get PowerMockRule working, either with xstream of objenesis.

A) I get this error when using:

powermock-module-junit4-rule-1.4.10.jar 
powermock-mockito-1.4.10-full.jar 
powermock-classloading-objenesis-1.4.10.jar
java.lang.NullPointerException
        at java.lang.reflect.Field.get(Unknown Source)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:229)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
        at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
        at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:82)
        at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
        at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

B) And this error when I use:

powermock-module-junit4-rule-1.4.10.jar 
powermock-mockito-1.4.10-full.jar 
powermock-classloading-xstream-1.4.10.jar 
xstream-1.4.2.jar 
xpp3_min-1.1.4c.jar xmlpull-1.1.3.1.jar
com.thoughtworks.xstream.converters.ConversionException: javassist.NotFoundException: $Proxy4 : javassist.NotFoundException: $Proxy4
---- Debugging information ----
message             : javassist.NotFoundException: $Proxy4
cause-exception     : java.lang.RuntimeException
cause-message       : javassist.NotFoundException: $Proxy4
class               : java.lang.Class
required-type       : java.lang.Class
converter-type      : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter   : com.thoughtworks.xstream.converters.extended.JavaClassConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/next/fTarget/applicationContext/classLoader/classes/java-class[56]
line number         : 1152
class[1]            : java.util.Vector
converter-type[1]   : com.thoughtworks.xstream.converters.collections.CollectionConverter
class[2]            : sun.misc.Launcher$AppClassLoader
converter-type[2]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[3]            : org.springframework.context.support.GenericApplicationContext
class[4]            : org.ibit.rol.sac.persistence.ejb.UnidadAdministrativaFacadeEJBTest
class[5]            : org.junit.internal.runners.statements.InvokeMethod
class[6]            : org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks
class[7]            : org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks
class[8]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[9]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : null
-------------------------------
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
        at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:71)
        at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:79)
        at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72)
        at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66)
        at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
        at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
        at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
        at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
        at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: javassist.NotFoundException: $Proxy4
        at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)
        at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)
        at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at com.thoughtworks.xstream.core.util.ClassLoaderReference.loadClass(ClassLoaderReference.java:31)
        at com.thoughtworks.xstream.converters.extended.JavaClassConverter.loadClass(JavaClassConverter.java:67)
        at com.thoughtworks.xstream.converters.extended.JavaClassConverter.fromString(JavaClassConverter.java:44)
        at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.fromString(SingleValueConverterWrapper.java:41)
        at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.unmarshal(SingleValueConverterWrapper.java:49)
        at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        ... 88 more
Caused by: javassist.NotFoundException: $Proxy4
        at javassist.ClassPool.get(ClassPool.java:436)
        at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:180)
        ... 97 more

Any help?
Comment by royce....@gmail.com, May 9, 2012

I too am not able to make the example work. I am trying to make the SpringJUnit4ClassRunner work with PowerMock? and Mockito.

java.lang.IllegalStateException?: Failed to transform class with name ... Reason: duplicate method: <init>

I am using the latest version Mockito 1.9, PowerMock? 1.4.12, javassist 3.12.

Any ideas?

Comment by lukewpat...@gmail.com, May 22 (3 days ago)

@Rule-annotated field must be public for JUnit to use it.


Sign in to add a comment
Powered by Google Project Hosting