Unbelievable: Cannot cast from class X to its super class

不羁的心 提交于 2019-12-05 17:57:27

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

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

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.*" })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!