first reaction, might be worth packaging up the construction of the url into a collaborator. If you also package up the handling of the XML, then maybe you could just write an integration test for the use of the slurper. Just different options...
S
On 9 Jan 2013, at 21:41, Blake Boesinger wrote:
> Does injecting a type you don't own, for example an XmlSlurper, indicate a
> design problem?
> Should you just wrap it in a type that more closely fits your domain and if
> so how do you justify the extra level of abstraction?
>
> Also, when should you break the "only mock types you own" rule?
>
>
> The code in question looks something like the following.
> I'm struggling to justify wrapping the xmlSlurper.
>
> class NameChangedToProtectTheInnocentScheduleLoader implements ScheduleLoader {
>
> def endpoint
> def calendar
> def xmlSlurper
>
> @Override
> public Schedule load() {
> def yesterday = calendar.yesterday()
> def url = endpoint + yesterday +'.xml'
> def xml = xmlSlurper.parse(url)
>
> *//do stuff with the XML*
> }
>
>
>
> The test looks something like this
>
> def "should retrieve yesterday's XML schedule from name changed to protect the innocent"() {
> given:
> def calendar = Mock(Calendar)
> calendar.yesterday() >> "06-02-2011"
>
> def xmlSlurper = Mock(XmlSlurper)
> def path = Mock(GPathResult, constructorArgs: [null, null, null, null])
> xmlSlurper.parse("
http://example.org/06-02-2011.xml") >> path
> path.getProperty("ContentStoreID") >> ["test"]
>
> def loader = new NameChangedToProtectTheInnocentScheduleLoader(calendar: calendar, endpoint: TEST_ENDPOINT, xmlSlurper: xmlSlurper)
> when:
> def schedule = loader.load()
> then:
> assert schedule?.productions[0]?.id == new FileFriendlyId("test")
> }
>
>
>
> All the XmlSlurper bits feel wrong.
> To fix it do I just wrap it in my own class?
> If so, how do I test that? Leave it to the acceptance tests?
> It seems tricky to test with an integration test.
>
>
Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006
Book:
http://www.growing-object-oriented-software.com
+44 797 179 4105
Twitter: @sf105
Higher Order Logic Limited
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 7522677