Junit 测试预期结果的 Xml 字符串表示形式
我正在编写一个 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");
}
I'm writing a Junit test in which the expected result is a XML string. What's the best way represent the expected string? Right now I have it as a stringBuilder and I do the following. I want to assert that the actual and expected are same. What would be the most efficient way to represent this expected string? Also I don't want anyone to alter the expected string so it should be final too.
@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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
a) XML 不属于 java 代码。将其放在同一个包中(如果使用 maven,最好放在 src/main/resources 文件夹中)并将其作为资源加载。
b) 不要与 XML 进行字符串比较:进行 XML 结构比较。 规范化器是个好主意。
a) XML does not belong inside java code. Put it in the same package (preferably in the src/main/resources folder if you use maven) and load it as a resource.
b) Don't do a string comparison with XML: do an XML structure comparison. The canonicalizer is a great idea.
这比看起来更难——字符串比较肯定会失败。这是因为至少
最简单的方法是规范化预期的 XML,并测试和比较它们。所有好的 XML 工具都应该有一个规范化器。
我必须编写大量 XML 机制来支持我的单元测试 - 诚然,它们包含浮点数,并且我必须允许舍入错误。
但比较字符串肯定会失败。
This is harder than it looks - string comparisons will certainly fail. This is because at least
The simplest way is to canonicalise the XML in both the expected and test and compare those. All good XML tools should have a canonicalizer.
I have had to write a lot of XML machinery to support my Unit tests - admittedly they contain floating point numbers and I have to allow for rounding errors.
But comparing strings will certainly fail.
使用字符串表示形式比较 2 个 XML 是危险的。事实上,您认为以下 XML :
等于:
吗?
如果是,那么我建议您使用 XMLUnit 来测试当前的 XML 与预期的 XML。
关于预期的 XML,最好的方法是将它们存储为外部文件,并在测试期间加载它们(例如在
@BeforeClass
代码段中)。Comparing 2 XML using their String representation is dangerous. Indeed, do you consider the following XML :
equals to:
?
If yes, then I suggest that you use XMLUnit to test your current XML with the expected one.
Regarding the expected XML, the best way is to store them as external files, and load them during your test (in a
@BeforeClass
snippet for example).我认为你走在正确的轨道上——定义一个字符串常量“EXPECTED_XML”并断言它。
I think you are on the correct track -- define a string constant "EXPECTED_XML" and just assert off that.