Compare scala.xml.Elem object in unit test

前端 未结 4 1900
执笔经年
执笔经年 2021-02-19 06:21

I have two scala.xml.Elem objects (actual, expected). I am using JUnit 4, but have also included XMLUnit 1.3.

Is there any easy way to compare the two objec

4条回答
  •  清歌不尽
    2021-02-19 06:54

    The earlier answers were helpful to me, though I found that sometimes I wanted to check a larger chunk of XML and the failure comparison showing both chunks of XML was a bit hard to read. This method will try to recurse down into child elements first to compare those, so if a deeply nested element is incorrect it will show a much more concise error. Depending on your XML this might not give you enough context to work out where it's actually failing, but I find it useful.

    /** Check that the XMLs are the same, ignoring empty text nodes (whitespace). */
    private def assertEqual(actual: xml.Node, expected: xml.Node) {
    
        def recurse(actual: xml.Node, expected: xml.Node) {
            // depth-first checks, to get specific failures
            for ((actualChild, expectedChild) <- actual.child zip expected.child) {
                recurse(actualChild, expectedChild)
            }
            actual should be (expected)
        }
    
        recurse(scala.xml.Utility.trim(actual), scala.xml.Utility.trim(expected))
    
    }
    

提交回复
热议问题