Powermock + Mockito + Spring = DocumentBuilderFactoryImpl

2,663 views
Skip to first unread message

Chrisnofneur

unread,
May 28, 2009, 3:33:39 AM5/28/09
to PowerMock
Hi

I try to use PowerMock & Mockito together to mock a static method of a
custom class.
In this test, I also load an Application Context using Spring.

When launching the test, I got a ClassCastException during the
ApplicationContext creation :

this.appCtx = new ClassPathXmlApplicationContext(new String[]
{"beans.xml"});

Any ideas ?

Thanks

Chris

The exception :

org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from class path resource
[beans-moderation.xml]; nested exception is
java.lang.ClassCastException:
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to
javax.xml.parsers.DocumentBuilderFactory
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions
(XmlBeanDefinitionReader.java:420)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions
(XmlBeanDefinitionReader.java:342)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions
(XmlBeanDefinitionReader.java:310)
at
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:143)
at
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:178)
at
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:149)
at
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions
(AbstractBeanDefinitionReader.java:212)
at
org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions
(AbstractXmlApplicationContext.java:113)
at
org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions
(AbstractXmlApplicationContext.java:80)
at
org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory
(AbstractRefreshableApplicationContext.java:123)
at
org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory
(AbstractApplicationContext.java:423)
at
org.springframework.context.support.AbstractApplicationContext.refresh
(AbstractApplicationContext.java:353)
at
org.springframework.context.support.ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:139)
at
org.springframework.context.support.ClassPathXmlApplicationContext.<init>
(ClassPathXmlApplicationContext.java:93)
at my.app.bla.kernel.mgr.bla.MyTestClass.setUp(MyTestClass.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.MethodRoadie.runBefores
(MethodRoadie.java:122)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters
(MethodRoadie.java:86)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$2.runBeforesThenTestThenAfters
(PowerMockJUnit44RunnerDelegateImpl.java:212)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod
(PowerMockJUnit44RunnerDelegateImpl.java:205)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods
(PowerMockJUnit44RunnerDelegateImpl.java:159)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:133)
at org.junit.internal.runners.ClassRoadie.runUnprotected
(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected
(ClassRoadie.java:37)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run
(PowerMockJUnit44RunnerDelegateImpl.java:131)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run
(JUnit4TestSuiteChunkerImpl.java:112)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run
(AbstractCommonPowerMockRunner.java:44)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)
Caused by: java.lang.ClassCastException:
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to
javax.xml.parsers.DocumentBuilderFactory
at javax.xml.parsers.DocumentBuilderFactory.newInstance
(DocumentBuilderFactory.java:123)
at
org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory
(DefaultDocumentLoader.java:89)
at
org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument
(DefaultDocumentLoader.java:70)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions
(XmlBeanDefinitionReader.java:396)
... 37 more

Johan Haleby

unread,
May 28, 2009, 3:41:21 AM5/28/09
to powe...@googlegroups.com
Hi,

Try to use @PowerMockIgnore("org.apache.xerces") at the class-level of your test. Please let us know if it works or not.

/Johan

Chrisnofneur

unread,
May 28, 2009, 3:59:26 AM5/28/09
to PowerMock
Hi Johan,

Thanks for this quick answer,

I added @PowerMockIgnore("org.apache.xerces") but nothing changed, I
still catch the same exception.

(...)

@PowerMockIgnore("org.apache.xerces") << I added this
line
@RunWith(PowerMockRunner.class)
@PrepareForTest(User.class)
public class MyTestClass {

(...)


On May 28, 9:41 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Try to use @PowerMockIgnore("org.apache.xerces") at the class-level of your
> test. Please let us know if it works or not.
>
> /Johan
>

Johan Haleby

unread,
May 28, 2009, 4:40:46 AM5/28/09
to powe...@googlegroups.com
Hi,

You're using the annotation the right way. Try to ignore "javax.xml.parsers" instead and see if that works. If not try to ignore "org.springframework.beans" or a combination of the these.

Unfortunately this is a known side-effect with using PowerMock with frameworks that instantiates object using reflection with a specific class-loader.

/Johan

Chrisnofneur

unread,
May 28, 2009, 5:21:11 AM5/28/09
to PowerMock
Hi,

Thanks again but it still got exception !
As I am running out of time, I give up with PowerMock for the moment
and go on will the old way (create an intermediate class making the
static call, and mock it)

Thanks a lot for your quick help !

I'll try again PowerMock another time

Chris

On May 28, 10:40 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> You're using the annotation the right way. Try to ignore "javax.xml.parsers"
> instead and see if that works. If not try to ignore
> "org.springframework.beans" or a combination of the these.
>
> Unfortunately this is a known side-effect with using PowerMock with
> frameworks that instantiates object using reflection with a specific
> class-loader.
>
> /Johan
>

Johan Haleby

unread,
May 28, 2009, 6:31:12 AM5/28/09
to powe...@googlegroups.com
Ok I'm sorry to hear that. Unfortunately you've run into a special case which should be something that you don't normally have to do when mocking static methods. I'm sure that it's possible to solve it though.

/Johan

Julio Cesar Fernandes Corrêa

unread,
Jun 27, 2013, 1:07:05 PM6/27/13
to powe...@googlegroups.com
Here is something that worked for me:

@PowerMockIgnore({"javax.xml.*", "org.xml.sax.*", "org.w3c.dom.*",  "org.springframework.context.*", "org.apache.log4j.*"}) 
Reply all
Reply to author
Forward
0 new messages