在单元测试中比较scala.xml.Elem对象 [英] Compare scala.xml.Elem object in unit test
问题描述
我有两个scala.xml.Elem
对象(实际,预期).我正在使用JUnit 4,但还包括XMLUnit 1.3.
I have two scala.xml.Elem
objects (actual, expected). I am using JUnit 4, but have also included XMLUnit 1.3.
有没有一种简单的方法可以比较两个对象是否相等,而忽略XML中的属性顺序和无关紧要的空白?
Is there any easy way to compare the two objects for equality, ignoring attribute order and insignificant whitespace in the XML?
我尝试了XMLUnit.assertXMLEqual()
,但是它抱怨类型是scala.xml.Elem
.
I tried XMLUnit.assertXMLEqual()
, but it complains that the types are scala.xml.Elem
.
我知道我可以使用equals
或==
,但是我想让断言在两个值不相等时打印两个值的好处.如果我使用assertTrue(actual.equals(expected))
且它们不相等,则唯一的输出将是断言失败".
I know that I can use equals
or ==
, but I would like the benefit of having the assertion print the two values when they are not equal. If I use assertTrue(actual.equals(expected))
, and they are not equal, the only output will be "assertion failed".
推荐答案
使用允许传递自定义消息的assertTrue
版本
Use the version of assertTrue
that allows passing custom messages
public static void assertTrue(java.lang.String message,
boolean condition)
和(例如)diff
以产生具有不相等的后代和节点的字符串
and (for example) diff
to produce the string with the descendand nodes that aren't equal
scala> val xml1 = <person><name>john</name><lastname>smith</lastname></person>
xml1: scala.xml.Elem = <person><name>john</name><lastname>smith</lastname></person>
scala> val xml2 = <person><name>jane</name><lastname>smith</lastname></person>
xml2: scala.xml.Elem = <person><name>jane</name><lastname>smith</lastname></person>
scala> assert(xml1 == xml2, xml1.child diff xml2.child mkString(", "))
java.lang.AssertionError: assertion failed: <name>john</name>
at scala.Predef$.assert(Predef.scala:91)
at .<init>(<console>:8)
at .<clinit>(<console>)
这篇关于在单元测试中比较scala.xml.Elem对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!