5

我正在尝试使用包含所有发现的差异的(文本)报告来覆盖默认的 XMLUnit 行为,即仅报告两个输入之间发现的第一个差异。

到目前为止,我已经完成了这个:

private static void reportXhtmlDifferences(String expected, String actual) {
  Diff ds = DiffBuilder.compare(Input.fromString(expected))
    .withTest(Input.fromString(actual))
    .checkForSimilar()
    .normalizeWhitespace()
    .ignoreComments()
    .withDocumentBuilderFactory(dbf).build();

  DefaultComparisonFormatter formatter = new DefaultComparisonFormatter();
  if (ds.hasDifferences()) {
    StringBuffer expectedBuffer = new StringBuffer();
    StringBuffer actualBuffer = new StringBuffer();
    for (Difference d: ds.getDifferences()) {
      expectedBuffer.append(formatter.getDetails(d.getComparison().getControlDetails(), null, true));
      expectedBuffer.append("\n----------\n");

      actualBuffer.append(formatter.getDetails(d.getComparison().getTestDetails(), null, true));
      actualBuffer.append("\n----------\n");
    }
    throw new ComparisonFailure("There are HTML differences", expectedBuffer.toString(), actualBuffer.toString());
  }
}

但我不喜欢:

  1. 必须遍历Differences客户端代码。
  2. 进入该比较类型的内部并使用该类型进行DefaultComparisonFormatter调用。getDetailsnull
  3. 用虚线连接差异。

也许这只是来自一种不合理的坏直觉,但我想知道是否有人对这个用例有一些意见。

4

1 回答 1

0

XMLUnit 建议简单地打印出差异,请参阅“旧 XMLUnit 1.x 详细差异”部分:https ://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to- 2.x

您的代码如下所示:

private static void reportXhtmlDifferences(String expected, String actual) {
  Diff ds = DiffBuilder.compare(Input.fromString(expected))
    .withTest(Input.fromString(actual))
    .checkForSimilar()
    .normalizeWhitespace()
    .ignoreComments()
    .withDocumentBuilderFactory(dbf).build();

  if (ds.hasDifferences()) {
    StringBuffer buffer = new StringBuffer();
    for (Difference d: ds.getDifferences()) {
      buffer.append(d.toString());
    }
    throw new RuntimeException("There are HTML differences\n" + buffer.toString());
  }
}
于 2019-05-20T11:27:44.290 回答