JUnit可以在测试结束时比较两个XML文件并进行差异吗?有没有人有这样的在线示例?
答案 0 :(得分:0)
也许这对你有所帮助,也许不是。如果你想要辨别一个小的差异,那么这个简单的例子可能会成功。我喜欢AssertJ以获得流畅的断言,但也因为它提供了一些有用的失败消息!
package example;
import static org.assertj.core.api.Assertions.*;
import com.google.common.base.Joiner;
import org.junit.Test;
public class XmlDiffExample {
private static final String NEWLINE = System.getProperty("line.separator");
private Joiner j = Joiner.on(NEWLINE);
private String[] xml = new String[] {
"<?xml version='1.0'?>",
"<container>",
"<greeting>hello, %s!</greeting>",
"</container>"
};
@Test
public void should_diff_xml_docs() {
assertThat(getDoc1()).isEqualTo(getDoc2());
}
private String getDoc1() {
return String.format(j.join(xml), "world");
}
private String getDoc2() {
return String.format(j.join(xml), "foo");
}
}
的产率:
org.junit.ComparisonFailure: expected:<...>
<greeting>hello, [foo]!</greeting>
</cont...> but was:<...>
<greeting>hello, [world]!</greeting>
</cont...>