javax.xml.parsers.SAXParserFactory ClassCastException [英] javax.xml.parsers.SAXParserFactory ClassCastException

查看:185
本文介绍了javax.xml.parsers.SAXParserFactory ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过maven(mvn test)运行测试时,我在本地计算机上遇到以下异常。

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

在谷歌搜索后,我遇到了几个描述其背后主要问题的页面(几个SAXParserFactoryImpl在不同的类加载器中。)

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/

我的问题是,如何确定哪个库也提供了SAXParserFactoryImpl,以便我可以排除它。我正在使用Maven,IntelliJ和JDK 1.6.0_23。问题出现在命令行以及从IntelliJ运行测试时。

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.

更新1

我第一次运行mvn测试时想通了在mvn clean之后,错误不会出现。但是一旦我再次运行mvn test(没有干净,就会发生异常)(当我从IntelliJ运行它时)。

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

当我在cmd行上运行它时,几个mvn测试调用就可以了。

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

推荐答案

我发现了这个问题。它与试图加载SAXParserFactory的PowerMockito有关。我之所以没有想到这一点是因为堆栈跟踪仅包含两次PowerMockito,而这是在中间: - )

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 :-)

所以如果你弄清楚这个问题在IntelliJ中,您确实使用PowerMockito,使用以下注释注释您的测试类:

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.*"])

这解决了我的问题。

这篇关于javax.xml.parsers.SAXParserFactory ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆