Unbelievable: Cannot cast from class X to its super class
Asked Answered
N

4

5

I'm encountering a very weird problem with Spring (3.0.1.RELEASE), TestNG (5.11) and Maven Surefire (2.5). I have a test class that extends a Spring helper class for testNG so that the test context can be loaded from an xml file (that contains some bean definitions). My project was imported into eclipse using m2eclipse (using Import Maven Project). The class runs fine in Eclipse TestNG runner. However, it throws this exception with Maven Surefire:

Caused by: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.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:388)

I have eliminated all involved dependencies in my pom so that the two classes com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl and javax.xml.parsers.DocumentBuilderFactory are coming from JRE only (the rt.jar).

So, it looks so unbelievable to me.

I wonder if there is any mechanism in loading class that can explain for this behavior?

Thanks.

Updated: Here is my dependency tree:

[INFO] org.seamoo:seamoo-webapp:war:1.0-SNAPSHOT
[INFO] +- com.google.appengine:appengine-api-1.0-sdk:jar:1.3.1:compile
[INFO] +- com.google.gwt:gwt-servlet:jar:2.0.3:compile
[INFO] +- com.google.gwt:gwt-user:jar:2.0.3:provided
[INFO] +- org.apache.geronimo.specs:geronimo-servlet_2.5_spec:jar:1.2:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.5.6:compile
[INFO] +- ch.qos.logback:logback-classic:jar:0.9.15:compile
[INFO] |  \- ch.qos.logback:logback-core:jar:0.9.15:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-asm:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:3.0.1.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-expression:jar:3.0.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:3.0.1.RELEASE:compile
[INFO] |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-context-support:jar:3.0.1.RELEASE:compile
[INFO] +- org.apache.tiles:tiles-jsp:jar:2.1.4:compile
[INFO] |  \- org.apache.tiles:tiles-servlet:jar:2.1.4:compile
[INFO] +- org.apache.tiles:tiles-portlet:jar:2.1.4:compile
[INFO] |  \- org.apache.tiles:tiles-core:jar:2.1.4:compile
[INFO] |     +- org.apache.tiles:tiles-api:jar:2.1.4:compile
[INFO] |     +- commons-digester:commons-digester:jar:1.8.1:compile
[INFO] |     |  \- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |     \- commons-logging:commons-logging-api:jar:1.1:compile
[INFO] +- org.apache.tomcat:jasper-el:jar:6.0.26:compile
[INFO] |  \- org.apache.tomcat:el-api:jar:6.0.26:compile
[INFO] +- org.freemarker:freemarker:jar:2.3.16:compile
[INFO] +- org.tuckey:urlrewritefilter:jar:3.1.0:compile
[INFO] +- org.seamoo.utils:converter:jar:0.1:compile
[INFO] +- junit:junit:jar:4.6:test
[INFO] +- org.springframework:spring-test:jar:3.0.1.RELEASE:test
[INFO] +- org.testng:testng:jar:jdk15:5.11:test
[INFO] +- net.sourceforge.htmlunit:htmlunit:jar:2.7:test
[INFO] |  +- commons-collections:commons-collections:jar:3.2.1:test
[INFO] |  +- commons-lang:commons-lang:jar:2.4:test
[INFO] |  +- commons-httpclient:commons-httpclient:jar:3.1:test
[INFO] |  +- commons-codec:commons-codec:jar:1.4:test
[INFO] |  +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.7:test
[INFO] |  +- net.sourceforge.nekohtml:nekohtml:jar:1.9.14:test
[INFO] |  +- net.sourceforge.cssparser:cssparser:jar:0.9.5:test
[INFO] |  |  \- org.w3c.css:sac:jar:1.3:test
[INFO] |  \- commons-io:commons-io:jar:1.4:test
[INFO] +- org.jbehave:jbehave-core:jar:2.5:test
[INFO] |  +- junit:junit-dep:jar:4.4:test
[INFO] |  \- org.hamcrest:hamcrest-all:jar:1.1:test
[INFO] +- org.mockito:mockito-all:jar:1.8.4:test
[INFO] +- org.powermock.modules:powermock-module-testng:jar:1.3.7:test
[INFO] |  \- org.powermock:powermock-core:jar:1.3.7:test
[INFO] |     +- org.powermock.reflect:powermock-reflect:jar:1.3.7:test
[INFO] |     |  \- org.objenesis:objenesis:jar:1.2:test
[INFO] |     \- javassist:javassist:jar:3.10.0.GA:test
[INFO] +- org.powermock.api:powermock-api-mockito:jar:1.3.7:test
[INFO] |  \- org.powermock.api:powermock-api-support:jar:1.3.7:test
[INFO] +- org.workingonit:gwtbridge:jar:0.1:compile
[INFO] +- org.seleniumhq.selenium.client-drivers:selenium-java-testng-helper:jar:1.0.1:test
[INFO] |  \- org.seleniumhq.selenium.client-drivers:selenium-java-client-driver:jar:1.0.1:test
[INFO] +- com.dyuproject:dyuproject-util:jar:1.1-SNAPSHOT:compile
[INFO] +- com.dyuproject:dyuproject-openid:jar:1.1-SNAPSHOT:compile
[INFO] +- org.mortbay.jetty:jetty-util:jar:6.1.22:compile
[INFO] +- org.seamoo:seamoo-model:jar:1.0-SNAPSHOT:compile
[INFO] |  +- javax.jdo:jdo2-api:jar:2.3-eb:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- com.google.appengine:geronimo-jpa_3.0_spec:jar:1.1.1:compile
[INFO] +- org.seamoo:seamoo-persistence:jar:1.0-SNAPSHOT:compile
[INFO] |  +- com.google.appengine.orm:datanucleus-appengine:jar:1.0.5.final:compile
[INFO] |  +- org.datanucleus:datanucleus-core:jar:1.1.5:compile
[INFO] |  \- com.google.appengine:datanucleus-jpa:jar:1.1.5:runtime
[INFO] +- org.seamoo:seamoo-theme:jar:1.0-SNAPSHOT:compile
[INFO] \- org.seamoo:seamoo-test-resources:jar:1.0-SNAPSHOT:test
[INFO]    +- com.google.appengine:appengine-api-labs:jar:1.3.1:test
[INFO]    +- com.google.appengine:appengine-api-stubs:jar:1.3.1:test
[INFO]    \- com.google.appengine:appengine-testing:jar:1.3.1:test

I can sure that I have eliminate all possible jars because when I use Open Type feature of Eclipse, it suggests only 1 class for each of DocumentBuilderFactory and DocumentBuilderFactoryImpl

Nuthatch answered 12/4, 2010 at 4:29 Comment(4)
You say that both classes are coming from the JRE, but with this error, it really looks like DocumentBuilderFactory is coming from somewhere else. Can you dump the classpath to make sure that there are no jar files that you do not know about?Barby
I don't know how to dump the classpath, but I included my dependencies tree into the question (Wonder if it can help?). I use eclipse+m2eclipse and figure out which jars a class come from by typing the name into the Open Types dialog.Thrift
Ouch! It looks like PowerMock (on Mockito) doesn't play well with Spring Autowiring. I'm submiting the issue to PowerMock.Thrift
Thanks for figuring out it was PowerMock. Ran into the same issue but I was able to use @PowerMockIgnore on it.Subvert
P
3

I had a similar problem with DocumentBuilderFactoryImpl. I also initially assumed the cause was a conflict in the classpath, but it was actually a result of PowerMock creating an instrumented version of the class (and having two incompatible versions of the class in the classloader). I had no luck using the @PowerMockIgnore, but eventually fixed the problem by moving any object using JAXP into non-static methods (e.g. using @Before and not @BeforeClass to instantiate the object).

Pitchstone answered 28/1, 2011 at 12:8 Comment(1)
+1 Thanks! This was my issue: I had some inheritence model with statics that were clearly being recreated via @Before and through PowerMock's black magic.Stannic
I
1

You're most likely hitting a classloader problem. Run the tests with -verbose:class to see where the classes are loaded from.

Incommutable answered 12/4, 2010 at 7:19 Comment(3)
Here is what I got:<br/> $mvn test -verbose:class<br/> Apache Maven 2.2.1 (r801777; 2009-08-07 02:16:01+0700)<br/> Java version: 1.6.0_18<br/> Java home: /home/phuongnd08/app/jvm/jdk1.6.0_18/jre<br/> Default locale: en_US, platform encoding: UTF-8<br/> OS name: "linux" version: "2.6.31-20-generic" arch: "amd64" Family: "unix"<br/> Look like I didn't got you. Please advice.Thrift
@Phuong : perhaps MAVEN_OPTS='-verbose:class' mvn test will work.Incommutable
I tried your commands and it shows me several classes and which jars class loaded from. But, to my surprise, DocumentBuilderFactory and DocumentBuilderFactoryImpl are not listed until the exception is thrown. Would you please see my log uploaded here: docs.google.com/…Thrift
I
1

I had the same problem with SAXParserFactoryImpl and Powemock.

javax.xml.parsers.FactoryConfigurationError: Provider com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl could not be instantiated: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory

I was able to fix the problem by ignoring respective classes from Powermock like this:

@PowerMockIgnore({ "org.apache.xerces.*", "javax.xml.parsers.*", "org.xml.sax.*" })
Ichthyic answered 16/11, 2015 at 9:7 Comment(1)
I also had to ignore some org.w3c.dom @PowerMockIgnore({"org.apache.log4j.*", "org.apache.xerces.*", "javax.xml.parsers.*", "org.w3c.dom.*", "org.xml.sax.*", "com.sun.org.apache.xerces.*"})Claudine
C
1

Alternative of

@PowerMockIgnore({ "org.apache.xerces.*", "javax.xml.parsers.*", "org.xml.sax.*" })

is creation configuration.properties file in test/resources/org/powermock/extensions/ with content:

powermock.global-ignore=org.apache.xerces.*,javax.xml.parsers.*,org.xml.sax.*

Note: Comma separation and NO spaces in the property value, otherwise, it will not work!
The list of packages depends on the dependencies of your project.

Colner answered 5/4, 2021 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.