Java Reference
In-Depth Information
Figure 6.4 shows how I want the stub to work.
Figure 6.4. The geocoder relies on an XmlSlurper instantiated locally to do its job. The goal is to modify its
parse method to return the needed value, even though the slurper is a local variable in the test method.
I want the parse method of the slurper to return the root of a DOM tree that looks like
what the Google geocoder would have returned had I been able to access it. The easiest
way to get that value is to set up the proper XML tree and parse it ahead of time:
String xml = '''
<root><result><geometry>
<location>
<lat>37.422</lat>
<lng>-122.083</lng>
</location>
</geometry></result></root>'''
def correctRoot = new XmlSlurper().parseText(xml)
The XML string has the proper latitude and longitude for the Google home office. To get
the proper root value I invoke the parseText method of the XmlSlurper . My Geo-
coder class can take that root and walk the tree as usual to get the latitude and longitude.
The challenge is this: how do I get my own Geocoder to use this implementation when
there's no obvious way to inject it? The solution is to use the StubFor class and set the
expectations around it:
 
Search WWH ::




Custom Search