Using PowerMock in a parameterized JUnit test

2,262 views
Skip to first unread message

Sri Sankaran

unread,
Sep 21, 2010, 10:09:24 AM9/21/10
to PowerMock
I would like to use PowerMock to intercept the usage of a
constructor. However I am running into a problem because I am using a
parameterized JUnit test and therefore I am unable to specify the
PowerMock runner.

My test looked like this:

@RunWith(org.junit.runners.Parameterized.class)
public class TestClassName {
..
}

This prevents me from adding the "@RunWith(PowerMockRunner.class)"
annotation.

Any suggestions?

Johan Haleby

unread,
Sep 21, 2010, 12:09:48 PM9/21/10
to powe...@googlegroups.com
Hi,

You can try using the PowerMock rule.

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


Sri Sankaran

unread,
Sep 21, 2010, 3:56:40 PM9/21/10
to PowerMock
Johan:
Thanks for the suggestion. Adding the one line

@Rule public PowerMockRule powerMockRule = new PowerMockRule();

results in a strange exception at test startup.

It is an java.lang.ExceptionInInitializerError caused by

Caused by:
org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from class path resource
[applicationContext.xml]; nested exception is
java.lang.ClassCastException:
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
cannot be cast to javax.xml.parsers.DocumentBuilderFactory

Huh?! No I am not loading any custom XML parsers. If I remove the
"@Rule" line, the error goes away; of course PowerMock doesn't work
either.

Head scratching.

Sri

On Sep 21, 12:09 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> You can try using the PowerMock
> rule<http://code.google.com/p/powermock/wiki/PowerMockRule>
> .
>
> /Johan
>
> On Tue, Sep 21, 2010 at 4:09 PM, Sri Sankaran
> <srivaths.sanka...@gmail.com>wrote:
>
>
>
> > I would like to use PowerMock to intercept the usage of a
> > constructor.  However I am running into a problem because I am using a
> > parameterized JUnit test and therefore I am unable to specify the
> > PowerMock runner.
>
> > My test looked like this:
>
> > @RunWith(org.junit.runners.Parameterized.class)
> > public class TestClassName {
> >  ..
> > }
>
> > This prevents me from adding the "@RunWith(PowerMockRunner.class)"
> > annotation.
>
> > Any suggestions?
>
> > --
> > 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<powermock%2Bunsubscribe@googlegroups .com>
> > .

Johan Haleby

unread,
Sep 22, 2010, 2:21:22 AM9/22/10
to powe...@googlegroups.com
You get this error because somewhere in your code an XML framework is initialized. This framework tries to instantiate some classes or check for resources using reflection and this the context classloader is PowerMock's MockClassloader it cannot be found. What you need to do in these situations are to make use of the PowerMockIgnore annotation. Unfortunately this can be quite complex and lead to other unwanted side-effects. If you feel like the test-code is getting too complex you should refactor your code or think of another way to perform the test. But you could try out different combinations of PowerMockIgnore, e.g. @PowerMockIgnore("javax.xml.*") or @PowerMockIgnore("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"). Tell us how it goes.

/Johan


We ought add this to the FAQ very soon.

To unsubscribe from this group, send email to powermock+...@googlegroups.com.

Sri Sankaran

unread,
Sep 22, 2010, 9:59:16 AM9/22/10
to PowerMock
Thanks Johan again.

As you suspected this results in all kinds of problems.

Using no @PowerMockIgnore results in

org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from class path resource
[applicationContext.xml]; nested exception is
java.lang.ClassCastException:
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
cannot be cast to javax.xml.parsers.DocumentBuilderFactory

Using
@PowerMockIgnore("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl")
results in

org.springframework.beans.factory.BeanDefinitionStoreException:Unexpected
exception parsing XML document from class path resource
[applicationContext.xml]; nested exception is
java.lang.ClassCastException:com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
cannot be cast to javax.xml.parsers.DocumentBuilderFactory

Using @PowerMockIgnore("javax.xml.*") results in

org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from class path resource
[applicationContext.xml]; nested exception is java.lang.LinkageError:
loader constraint violation: when resolving method
"javax.xml.parsers.DocumentBuilder.setEntityResolver(Lorg/xml/sax/
EntityResolver;)V" the class loader (instance of org/powermock/core/
classloader/MockClassLoader) of the current class, org/springframework/
beans/factory/xml/DefaultDocumentLoader, and the class loader
(instance of <bootloader>) for resolved class, javax/xml/parsers/
DocumentBuilder, have different Class objects for the type org/xml/sax/
EntityResolver used in the signature

I am back to the drawing board :-(

Sri

On Sep 22, 2:21 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> You get this error because somewhere in your code an XML framework is
> initialized. This framework tries to instantiate some classes or check for
> resources using reflection and this the context classloader is PowerMock's
> MockClassloader it cannot be found. What you need to do in these situations
> are to make use of the PowerMockIgnore annotation. Unfortunately this can be
> quite complex and lead to other unwanted side-effects. If you feel like the
> test-code is getting too complex you should refactor your code or think of
> another way to perform the test. But you could try out different
> combinations of PowerMockIgnore, e.g. @PowerMockIgnore("javax.xml.*") or
> @PowerMockIgnore("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFa ctoryImpl").
> Tell us how it goes.
>
> /Johan
>
> We ought add this to the FAQ very soon.
>
> On Tue, Sep 21, 2010 at 9:56 PM, Sri Sankaran
> <srivaths.sanka...@gmail.com>wrote:
Reply all
Reply to author
Forward
0 new messages