打印XMLUnit中所有差异的惯用方法是什么?

Thi*_*ais 5 java xmlunit-2

我试图覆盖默认的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());
  }
}
Run Code Online (Sandbox Code Playgroud)

但我不喜欢:

  1. 必须遍历Differences客户端代码.
  2. 使用该ComparisonType 进入内部DefaultComparisonFormatter并调用.getDetailsnull
  3. 用线破折号来区分差异.

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

Fab*_*ann 0

XMLUnit 建议简单地打印出差异,请参阅“旧 XMLUnit 1.xDetailedDiff”部分:https://github.com/xmlunit/user-guide/wiki/Migration-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());
  }
}
Run Code Online (Sandbox Code Playgroud)