将Powerclass用于使用JAXB的测试类时发生链接错误

时间:2018-08-08 20:39:35

标签: java junit jaxb powermock powermockito

`java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshal

`Exception(Lorg/xml/sax/SAXException;)Ljavax/xml/bind/UnmarshalException;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, com/sun/xml/bind/v2/runtime/unmarshaller/UnmarshallerImpl, and its superclass loader (instance of <bootloader>), have different Class objects for the type org/xml/sax/SAXException used in the signature

我正在使用Powermock在JUNIT测试用例中模拟静态类。由于我的junit使用xml文件,因此出现的错误很少,因此必须使用powermock忽略,如下所示。

@PowerMockIgnore({ "javax.xml.*"})
@RunWith(PowerMockRunner.class)

我得到了链接错误以及以下错误。请指教

javax.xml.transform.TransformerFactoryConfigurationError: Provider for class javax.xml.transform.TransformerFactory cannot be created

1 个答案:

答案 0 :(得分:2)

在powermockignore中使用了所有三个导入后,问题解决了

@PowerMockIgnore({
        "javax.xml.*",
        "org.xml.*",
        "org.w3c.*"
})