难以置信:不能从X级演员到超级演员

时间:2010-04-12 04:29:57

标签: maven-2 spring testng surefire

我遇到了一个非常奇怪的问题:Spring(3.0.1.RELEASE),TestNG(5.11)和Maven Surefire(2.5)。 我有一个测试类,它扩展了testNG的Spring辅助类,以便可以从xml文件(包含一些bean定义)加载测试上下文。 我的项目使用m2eclipse导入eclipse(使用Import Maven Project) 该类在Eclipse TestNG运行器中运行良好。 但是,它会使用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)

我已经消除了我的pom中所有涉及的依赖项,以便这两个类 com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpljavax.xml.parsers.DocumentBuilderFactory仅来自JRE(rt.jar)。

所以,这对我来说太难以置信了。

我想知道加载类中是否有任何可以解释此行为的机制?

感谢。

更新:这是我的依赖树:

[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

我可以肯定我已经消除了所有可能的jar,因为当我使用Eclipse的Open Type功能时,它建议每个DocumentBuilderFactoryDocumentBuilderFactoryImpl只有1个类

4 个答案:

答案 0 :(得分:3)

我在DocumentBuilderFactoryImpl中遇到了类似的问题。我最初还假设原因是类路径中的冲突,但它实际上是PowerMock创建类的检测版本(并且在类加载器中具有两个不兼容的类版本)的结果。我没有运气使用@PowerMockIgnore,但最终通过使用JAXP将任何对象移动到非静态方法(例如使用@Before而不是@BeforeClass来实例化对象)来解决问题。

答案 1 :(得分:1)

您最有可能遇到类加载器问题。使用-verbose:class运行测试以查看类的加载位置。

答案 2 :(得分:1)

SAXParserFactoryImpl和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

我能够通过忽略Powermock中的各个类来解决问题:

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

答案 3 :(得分:0)

Alternative

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

正在 configuration.properties 中创建 test/resources/org/powermock/extensions/ 文件,内容为:

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

注意:属性值中逗号分隔,不能有空格,否则无效!
包列表取决于项目的依赖项。

相关问题