Cannot get PowerMock to run with ANT

965 views
Skip to first unread message

Graham Polley

unread,
Oct 25, 2011, 5:15:42 AM10/25/11
to PowerMock, alexei.v...@interactivedata.com, graham...@interactivedata.com
Hello,

We are using PowerMock to write some unit tests. When we use the
"@PrepareForTest" annotation to mock on a class with final methods,
ANT blows up with the following exception (below).

But the tests run fine from Eclipse JUnit plugin.

We have tried:

1) Using latest version of Javaassist (3.15.0-GA)
2) Using latest version of PowerMock
3) Checking that the build path is in the same order as ANT (read it
on another thread somewhere)
4) Moving the annotation from class level to test level
5) Removing the final keyword on the stubbed method
6) Checked that Eclipse and ANT are using the same JRE/JDK (1.6_25)

Any help much appreciated as we are running out of ideas!

Thanks.

Exception in thread "main" java.lang.IllegalStateException: Failed to
transform class with name [CLASS_NAME_HERE]. Reason: PowerMock
internal error when modifying field.
[junit] at
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:
207)
[junit] at
org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:
145)
[junit] at
org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:
65)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:247)
[junit] at
sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:
95)
[junit] at
sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:
107)
[junit] at
sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:
31)
[junit] at
sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:
370)
[junit] at
sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:
351)
[junit] at
sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:
653)
[junit] at
sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:
460)
[junit] at
sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:
286)
[junit] at
sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:
222)
[junit] at
sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:
69)
[junit] at
sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:
52)
[junit] at java.lang.Class.initAnnotationsIfNecessary(Class.java:
3070)
[junit] at java.lang.Class.getAnnotations(Class.java:3050)
[junit] at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.classAnnotations(PowerMockJUnit44RunnerDelegateImpl.java:
163)
[junit] at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.getDescription(PowerMockJUnit44RunnerDelegateImpl.java:
155)
[junit] at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getDescription(JUnit4TestSuiteChunkerImpl.java:
172)
[junit] at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.getDescription(AbstractCommonPowerMockRunner.java:
47)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:123)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:24)
[junit] at
org.junit.runners.ParentRunner.getDescription(ParentRunner.java:226)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:123)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:24)
[junit] at org.junit.runners.ParentRunner
$4.compare(ParentRunner.java:305)
[junit] at java.util.Arrays.mergeSort(Arrays.java:1270)
[junit] at java.util.Arrays.sort(Arrays.java:1210)
[junit] at java.util.Collections.sort(Collections.java:159)
[junit] at
org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:
285)
[junit] at
org.junit.runners.ParentRunner.getDescription(ParentRunner.java:225)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:123)
[junit] at org.junit.runners.Suite.describeChild(Suite.java:24)
[junit] at org.junit.runners.ParentRunner
$4.compare(ParentRunner.java:305)
[junit] at java.util.Arrays.mergeSort(Arrays.java:1270)
[junit] at java.util.Arrays.sort(Arrays.java:1210)
[junit] at java.util.Collections.sort(Collections.java:159)
[junit] at
org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:
285)
[junit] at
org.junit.runners.ParentRunner.getDescription(ParentRunner.java:225)
[junit] at org.junit.runner.Runner.testCount(Runner.java:38)
[junit] at
junit.framework.JUnit4TestAdapter.countTestCases(JUnit4TestAdapter.java:
35)
[junit] at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:
517)
[junit] at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:
1052)
[junit] at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:
906)
[junit] Caused by: java.lang.RuntimeException: PowerMock internal
error when modifying field.
[junit] at
org.powermock.core.transformers.impl.MainMockTransformer
$PowerMockExpressionEditor.edit(MainMockTransformer.java:257)
[junit] at javassist.expr.ExprEditor.loopBody(ExprEditor.java:
198)
[junit] at javassist.expr.ExprEditor.doit(ExprEditor.java:91)
[junit] at javassist.CtClassType.instrument(CtClassType.java:
1385)
[junit] at
org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:
75)
[junit] at
org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:
203)
[junit] ... 46 more
[junit] Caused by: javassist.NotFoundException:
javax.jnlp.BasicService
[junit] at javassist.ClassPool.get(ClassPool.java:440)
[junit] at
javassist.bytecode.Descriptor.toCtClass(Descriptor.java:592)
[junit] at
javassist.bytecode.Descriptor.toCtClass(Descriptor.java:542)
[junit] at javassist.CtField.getType(CtField.java:347)
[junit] at
org.powermock.core.transformers.impl.MainMockTransformer
$PowerMockExpressionEditor.edit(MainMockTransformer.java:255)
[junit] ... 51 more

Johan Haleby

unread,
Oct 25, 2011, 5:28:38 AM10/25/11
to powe...@googlegroups.com
Does it happen for all classes or is it just for a specific class?

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Graham Polley

unread,
Oct 25, 2011, 9:06:56 AM10/25/11
to powe...@googlegroups.com, alexei.v...@interactivedata.com
Hi Johan,

It does not happen for all classes. Just one (so far that we have found) in particular. But the source code is nothing out of the ordinary. Just some final methods. Basically, it is just wrapping a big HashMap and stuffing it with with startup configurations (e.g. resources, classes to load dynamically etc.) which can then be accessed globally by the application.

Mocking the class works fine e.g. Powermock.mock(THE_CLASS.class). But as soon as we use the "@PrepareForTest" annotation it breaks in ANT.

I would like to give you the source to look at but I can't for legal reasons!

Any help much appreciated.

Thanks,
Graham

Johan Haleby

unread,
Oct 25, 2011, 10:33:26 AM10/25/11
to powe...@googlegroups.com
Hmm, the problem could be that you extend HashMap actually. The problem is that HashMap is a java system and it's impossible for PowerMock to mock it. You could try to follow the technique described here but I'm not sure if it'll work since you're mocking a non-system class that extends a system class. Interesting :)

Personally I would probably not mock the class at all. If it extends HashMap just fill it with some values and use it normally without PowerMock (or any mocking framework).

/Johan

Graham Polley

unread,
Oct 25, 2011, 10:49:34 AM10/25/11
to powe...@googlegroups.com
We don't actually extend HashMap. We simply 'wrap' a HashMap so it is just a class variable inside the class.

We do need to mock it due to several reasons.

We are stumped currently!

Graham Polley

unread,
Oct 28, 2011, 6:03:18 PM10/28/11
to powe...@googlegroups.com

We are still unable to solve this. Has anyone got an idea?

Johan Haleby

unread,
Oct 29, 2011, 4:32:24 AM10/29/11
to powe...@googlegroups.com
Hmm I don't know really :/ The root cause seems to be that javax.jnlp.BasicService is not Found by the Javassist for some reason. Try using @PowerMockIgnore("javax.jnlp.*");

/Johan

Dusica Radojevic

unread,
Oct 29, 2011, 10:24:23 AM10/29/11
to powe...@googlegroups.com
I had similar problems earlier. I'm not sure, but you can check if you're missing some jars from test classpath. For example, it looks here that maybe j2ee.jar should be added to the test classpath. Sometimes when some third library uses some other libraries PowerMock is looking for definitions of classes from third library.

Graham Polley

unread,
Oct 31, 2011, 5:51:00 AM10/31/11
to powe...@googlegroups.com
Thanks guys. That the trick. We were missing javaws.jar from the classpath which contains javax.jnlp.BasicService

Much appreciated.
Reply all
Reply to author
Forward
0 new messages