VerifyError while JUnit test involving Document an

2019-08-21 11:36发布

问题:


MyClass

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
db = dbf.newDocumentBuilder(); //java.lang.verifyError here
Document doc = db.newDocument();

MyClassTest

documentBuilderFactory = PowerMockito.mock(DocumentBuilderFactory.class);
PowerMockito.mockStatic(DocumentBuilderFactory.class);  
PowerMockito.when(DocumentBuilderFactory.newInstance()).thenReturn(documentBuilderFactory);
document = PowerMockito.mock(Document.class);
//documentBuilder = PowerMockito.mock(DocumentBuilder.class);
//PowerMockito.when(documentBuilder.newDocument()).thenReturn(document);

When I'm removing the comment section from MyClassTest, the VerifyError comes in the last line of the test class. Any Idea how to solve this issue? I'm attaching the stack trace of the error.

java.lang.VerifyError: javax/xml/parsers/DocumentBuilder.newDocument()Lorg/w3c/dom/Document;

回答1:

java.lang.VerifyError can be the result when you have compiled against a different library than you are using at runtime.

It seems you have classpath issues. Fixing that should solve the issue. Make sure everywhere you have same version of jars.