我正在编写一个 Junit 测试,其中预期结果是一个 XML 字符串。表示预期字符串的最佳方式是什么?现在我把它作为一个 stringBuilder 并执行以下操作。我想断言实际和预期是相同的。表示此预期字符串的最有效方法是什么?此外,我不希望任何人更改预期的字符串,因此它也应该是最终的。
@Before
public void setUp() {
expected.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
expected.append("<n-relationship guid=\"IEEBFAD40BC5711DFAE41F5F92790F896\" control=\"add\">");
expected.append("<n-relbase>ID17A8B10BC5711DFAE41F5F92790F896</n-relbase><n-reltype>IPLS</n-reltype>");
expected.append("<ip.content><pub.no>1234567</pub.no>");
expected.append("<event.date>2010-09-10</event.date><event.code>02394802323</event.code>");
expected.append("<event.treatment>+</event.treatment><event.description>OPPOSITION DISMISSED</event.description>");
expected.append("</ip.content></n-relpayload></n-relationship");
}
最佳答案
a) XML 不属于 java 代码。将其放在同一个包中(如果使用 maven,最好放在 src/main/resources 文件夹中)并将其作为资源加载。
b) 不要与 XML 进行字符串比较:进行 XML 结构比较。 The canonicalizer是个好主意。
关于java - Junit 测试预期结果的 Xml 字符串表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4080242/