Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.xml.parsers.SAXParserFactory ClassCastException

I get on my local machine the following exception when running the tests by maven (mvn test).

ch.qos.logback.core.joran.event.SaxEventRecorder@195ed659 - Parser configuration error occured 
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory

After googling around I came across several pages which describe the main problem behind it (several SAXParserFactoryImpl in different classloaders).

-> http://www.xinotes.org/notes/note/702/

My question is, how can I figure out which library is also providing the SAXParserFactoryImpl, so that I can exclude it. I am using Maven, IntelliJ and JDK 1.6.0_23. The issue occurs on the command line as well as when running the tests from IntelliJ.

But the strange issue is, that on the build server this issue doesn't occur.

Update 1

Just figured out when I run the first time mvn test after an mvn clean, the error doesn't appear. But as soon as I run mvn test again (without clean, the exception occurs) (when I run it from IntelliJ).

When I run it on the cmd line, then several mvn test calls do work.

like image 517
rit Avatar asked Nov 18 '11 08:11

rit


1 Answers

I found the issue. It was related to PowerMockito who tried to load the SAXParserFactory. The reason why I haven't figured that one out was because the stacktrace contained only twice PowerMockito, and this at the middle :-)

So If you figure out this problem in IntelliJ and you do use PowerMockito, annotate your test class with the following annotation:

@PowerMockIgnore(["javax.management.*", "javax.xml.parsers.*",
         "com.sun.org.apache.xerces.internal.jaxp.*", "ch.qos.logback.*", "org.slf4j.*"])

This has solved the problem in my case.

like image 140
rit Avatar answered Oct 19 '22 08:10

rit