我试图覆盖默认的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)
但我不喜欢:
Differences客户端代码.DefaultComparisonFormatter并调用.getDetailsnull也许这只是来自一种不合理的坏直觉,但我想知道是否有人对这个用例有一些意见.
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)
| 归档时间: |
|
| 查看次数: |
373 次 |
| 最近记录: |