【问题标题】:Does XMLUnit have an assert to ignore whitespaceXMLUnit 是否有一个忽略空格的断言
【发布时间】:2011-04-19 18:59:52
【问题描述】:

我想在测试中比较两个 xml 字符串,但由于空格,测试一直失败。

@Test
public void testForEquality() throws Exception {
 String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
 String myTestXML = "<msg><uuid>0x00435A8C</uuid>      </msg>";
 assertXMLEqual(myControlXML, myTestXML);
 Diff diff = new Diff(myControlXML, myTestXML);
 assertTrue(diff.similar());
}

【问题讨论】:

  • 猜我应该再看 5 分钟 XMLUnit.setIgnoreWhitespace(true);

标签: java junit xmlunit


【解决方案1】:

是的,XMLUnit 可以忽略空格。有关详细信息,请参阅API documentation。您可以通过设置启用它:

XMLUnit.setIgnoreWhitespace(true)

【讨论】:

    【解决方案2】:

    API 已更改为 XMLUnit 2.x

    现在,对于单元测试,您可以使用 hamcrest 匹配器忽略空格,如下所示:

    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo;
    ...
    assertThat(actual, isIdenticalTo(expected).ignoreWhitespace());
    

    或者,直接使用构建器 API:

    import org.xmlunit.builder.DiffBuilder;
    ...
    boolean areDifferent = DiffBuilder.compare(left).withTest(right)
                                      .ignoreWhitespace().build().hasDifferences();
    

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      相关资源
      最近更新 更多