I try to write some JUnit tests for a Docx4J generator I have written. I want to compare the output node of my generator with an expected node that I want to load from a string.
So, I create my "actual" node (generator output) like so:
Node xmlNodeActual = XmlUtils.marshaltoW3CDomDocument(actual).getDocumentElement();
Where "actual" is the Object that was created by my generator.
For my "expected" node, I have written the following code:
Document doc = docBuilder.parse(new InputSource(new ByteArrayInputStream(strXmlNode.getBytes("utf-8")))):
Node xmlNodeExpected = doc.getDocumentElement();
strXmlNode is a string holding the expected xml. Although my two nodes are equal as far as I can tell from a visual diff, calling the following yields 'false' as a result:
xmlNodeActual.isEqualNode(xmlNodeExpected)
I suspect the reason is that the runtime types of the two nodes differ:
- xmlNodeActual: org.apache.xerces.dom.DeferredElementImpl
- xmlNodeExpected: org.apache.xerces.dom.ElementNSImpl
I like my test design since it would allow me to write a lot of test cases rather quickly for a large generator. However, I don't see a way to utilize this approach in combination with "isEqualNode". Do I have to write my own comparer or is there a way I am not aware of to make sure the types of the nodes are the same?