Search code examples
web-servicesrestgroovyxmlunit

Groovy and XMLUnit: compare webservice results


Using Groovy and XMLUnit I am trying to write a script to compare the xml output of web services from multiple endpoints. Attempting to get it working from one endpoint then iterate over endpoints to compare output, however, I continue to get the following error:

Caught: groovy.lang.GroovyRuntimeException: 
Could not find matching constructor for: 
     org.custommonkey.xmlunit.Diff(groovy.util.Node, groovy.util.Node)
groovy.lang.GroovyRuntimeException: 
Could not find matching constructor for: 
     org.custommonkey.xmlunit.Diff(groovy.util.Node, groovy.util.Node)

I am pretty sure it has to do with my inexperience with both XmlParser/XmlSlurper and XMLUnit (a.k.a. newbie). I greatly appreciate any pointers in the right direction. Here is sample code that causes the exception:

@Grab(group='xmlunit', module='xmlunit', version='1.5')
import org.custommonkey.xmlunit.*

def url = "http://www.webservicex.net//geoipservice.asmx/GetGeoIP?IPAddress=173.201.44.188"
def xmlParserResults = new XmlParser().parse("$url")
//same thing happens if I use...
//def xmlSlurperResults = new XmlSlurper().parse("$url")

def xmlDiff = new Diff(xmlParserResults, xmlParserResults)
assert xmlDiff.identical()

Thank you in advance!


Solution

  • The url returns xml and Diff takes two Strings to compare (in your case you are comparing Nodes). So the easiest way to compare would be to use URL instead of trying it to parse using XmlParser or XmlSlurper.

    def url = 
    "http://www.webservicex.net//geoipservice.asmx/GetGeoIP?IPAddress=173.201.44.188"
    def xmlString = new URL(url).text
    
    def xmlDiff = new Diff(xmlString, xmlString)
    assert xmlDiff.identical()
    

    In case the above is just a sample and not a working example of hitting multiple endpoints then the point is to represent the xml output as string and then compare.