• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XMLUnit ignoreWhitespace(true) NPE

 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using XMLUnit to do unit testing of XML output files. (Charles, thanks very much for the suggestion!) I've got it working pretty well, and the documentation is pretty good.

However, I have one problem that I can't find the answer to. I posted a question on the XMLUnit SourceForge forum a few days ago and haven't gotten any responses. (That forum is not very active.) So I thought I'd try here.

I am using Eclipse and have a small test project and my live project, build my Maven, that incorporates lots of third-party libraries. On my test project, I can set XMLUnit.setIgnoreWhitespace(true) and it works as advertised.

However, on my live project, I get a NullPointerException when I try to use this option. It works ok if this is set to false. Here is a partial stack trace of the NPE:

ava.lang.NullPointerException
at net.sf.saxon.Controller.prepareInputTree(Controller.java:1386)
at net.sf.saxon.Controller.transform(Controller.java:1318)
at org.custommonkey.xmlunit.Transform.transformTo(Transform.java:211)
at org.custommonkey.xmlunit.Transform.getResultDocument(Transform.java:233)
at org.custommonkey.xmlunit.Diff.getWhitespaceManipulatedDocument(Diff.java:187)
at org.custommonkey.xmlunit.Diff.getManipulatedDocument(Diff.java:209)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:154)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:144)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:108)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:100)
at org.custommonkey.xmlunit.Diff.<init>(Diff.java:92)
etc.

One difference in the two projects is that they are using different control parsers. If I do

System.out.println(XMLUnit.newControlParser().toString());

on each system, I get this on the test project that works:

com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl@xxxx

and this on the live project that NPE's:

org.apache.xerces.jaxp.DocumentBuilderImpl@xxxx

I'm not really sure what this means of if it's significant. I've tried using

XMLUnit.setControlParser(�com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl�)

but I think I've got the syntax wrong. This gives me the error:

avax.xml.parsers.FactoryConfigurationError: Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl could not be instantiated: java.lang.InstantiationException: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:104)
at org.custommonkey.xmlunit.XMLUnit.getControlDocumentBuilderFactory(XMLUnit.java:181)
at org.custommonkey.xmlunit.XMLUnit.setControlParser(XMLUnit.java:128)
etc.

Any ideas on how to fix this -- or perhaps where else to ask -- will be greatly appreciated. Thanks!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like your "live" and "test" projects have a different XML parser implementation. Are the Java versions identical? What about the classpaths?
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lasse, thank you very much for the tip. I resolved the issue as follows:

1. I discovered through trial and error that the problem arose because the live project had "saxon-8.7.jar" in the build path. When I added this to the test project, I got the same failure.
2. In the XMLUnit documentation, there is an example command to change the transformer factory used by XMLUnit as follows:

XMLUnit.setTransformerFactory("org.apache.xalan.processor.TransformerFactoryImpl");

In order for this to work, I had to add the "xalan.jar" file to the project. With this change, the setIgnoreWhitespace(true) works as advertised.

So, if I understand this correctly, it appears that the Transformer Factory provided by the saxon-8.7.jar is not compatible with XMLUnit but nevertheless becomes the default when available.

In any case, I've got a workable solution. Thanks for the tip on the classpath. Mark
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Thanks for sharing the solution for anyone who reads this in the future.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic