XMLUnit在为有效的XML创建Diff对象时抛出空指针异常。

huangapple go评论85阅读模式
英文:

XMLUnit throws NullPointerException when creating Diff object for valid xml

问题

我正在使用 org.custommonkey.xmlunit;(版本1.2)
在构建 Diff 对象时:
Diff diff = new Diff(expected, generated);

我总是遇到空指针异常,因为它在 this.controlDoc = this.getManipulatedDocument(controlDoc); 处失败。
在调试过程中,我发现在第一个构造函数中:

public Diff(String control, String test) throws SAXException, IOException {
    this((Reader)(new StringReader(control)), (Reader)(new StringReader(test)));
}

存在适当的 XML,然而当:

public Diff(Reader control, Reader test) throws SAXException, IOException {
    this(XMLUnit.buildDocument(XMLUnit.newControlParser(), control), XMLUnit.buildDocument(XMLUnit.newTestParser(), test));
}

被调用时,在调试器中我看到 [#document: null]。为什么会这样?我尝试过很多 XML,甚至在互联网上找到了一些非常简单的小XML,但都没有起作用。

英文:

I am using org.custommonkey.xmlunit; (version 1.2).
While building Diff object:
Diff diff = new Diff(expected, generated);

I always have NullPointerException as it's failing on this.controlDoc = this.getManipulatedDocument(controlDoc);.
While debugging I found that in first constructor:

public Diff(String control, String test) throws SAXException, IOException {
        this((Reader)(new StringReader(control)), (Reader)(new StringReader(test)));
    }

there are proper xmls however when:

public Diff(Reader control, Reader test) throws SAXException, IOException {
        this(XMLUnit.buildDocument(XMLUnit.newControlParser(), control), XMLUnit.buildDocument(XMLUnit.newTestParser(), test));
    }

is called I see in debugger [#document: null]. Why is that so? I tried with many xmls, even really easy and small which I found in internet but nothing works.

答案1

得分: 0

Oh, 我刚刚发现 XMLUnit.setIgnoreWhitespace(true); 引起了这个问题,因为在项目中我正在使用古老版本的 Saxon 库,而在版本 8.9 中已经修复了这个问题。然而这个:

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

非常有帮助。

英文:

Oh, I just have found that XMLUnit.setIgnoreWhitespace(true); cause this problem because in project I am using Saxon library in ancient version and it was fixed in version 8.9 however this:

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

helps a lot.

huangapple
  • 本文由 发表于 2020年4月7日 18:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61077927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定