Unit Testing Love Affair
July 5, 2006 · 8 Comments
I'm building a REST adapter right now - a CFC that offers an XML POST endpoint for REST web services that essentially wraps and exposes any remote methods on any CFCs you have on your server - and I'm absolutely in love with unit testing for this project!
The REST adapter exposes a single method. You POST an XML packet to it with a 'payload' argument whose root element names a component and has an attribute that specifies the method to call. Child XML elements specify the arguments to pass to the method. The REST adapter understands essentially the (Cold)Spring DTD XML definitions for values, lists, maps and beans and handles inbound arguments in that form and then encodes results in that form.
Because this is such a critical generic component, I've been writing test cases extensively - in advance of writing the actual code - so that I have a good degree of confidence in the resulting code.
It's been an interesting exercise. Write a test case. Run the test. It fails. Write the code in the REST adapter. Run the test. It passes. Well, in theory it does. Rinse and repeat. The nice thing about this is that you're thinking about the API and the error messages, not the implementation. You define the expected behavior, the concrete requirements... then you think about how to make it behave like that.
Try it, you might like it.
Download cfcUnit today!
p.s. I'll look into whether or not I can open source the REST adapter - I suspect other people may find it useful.
Tags: coldfusion · tdd

8 responses so far ↓
1 Rob Gonda // Jul 6, 2006 at 4:34 AM
2 Patrick Correia // Jul 6, 2006 at 8:02 AM
3 Luis Alvarez // Jul 21, 2006 at 7:24 AM
I just came across your article on REST Web services. I'm I correcting in asuming that this related to the ability to consume web services from coldfusion using HTTP Post and Get (binding node in the wsdl). I'm trying to produce a web service that can be consumed in this fashion, instead of RPC.
Thanks
-Luis
4 Sean Corfield // Jul 21, 2006 at 8:27 AM
5 Luis // Jul 21, 2006 at 8:48 AM
where exactly can I get the adaptor? I clicked on the link for cfunit thinking that it might be on the site.
-Luis
6 Sean Corfield // Jul 21, 2006 at 10:30 AM
As I said: "I'll look into whether or not I can open source the REST adapter - I suspect other people may find it useful."
7 Kurt // Aug 31, 2006 at 11:16 AM
Actually, I'd love to get your thoughts on how to design "Testable" software and how it relates to the whole Bean, DAO, Gateway, Service patterns. I'm battling between merging my Beans and DAOs to have a "Smart/Non-Anemic" model, and I'm also thinking about merging my Service and Gateway objects since services always seem to pass-through calls to my gateways. Obviously if I kept them all separate, I wouldn't have to worry about my persistence layer so much when testing, thus reducing the # of variables. However I'm in a mental battle over reducing "busy work" and maximizing cohesion, yet still keeping things maintainable. There's quite a battle going on :)
Anyway, I'd love to see your perspective on this sometime and was hoping you may have some good resources for me to check out.
Thanks for all the awesome blogging and community contributions, I don't know how you do it!
8 Sean Corfield // Sep 1, 2006 at 11:11 PM
Leave a Comment