WebService unit tests with extensive code generation.

The title got me your attention right? good, because this task – unit testing our WebService in one of our project – seemed to be a frazzled mission at first, but it’s amazing how good tools and healthy thinking able you to think about new ways to “assign” the redundant work to the one(and only) dude that never complains (though it makes noises here and there) – the computer !


Let me take a step back and explain a little bit about our WebService and the unit tests it was required:
We currently finished develop another application in our department and this application was required to expose an interface for “CRUD”ing(Create,Read,Update,Delete) several “System Tables”(“cities”,”countries”,”languages” etc) in the application. Eran(my teammate) was assigned for the job and he did a great job by well designing a general (XML)protocols for “talking” with the WS and by implementing the WS itself. After building the WebService and manually testing it he told me that his work was done and he’s ready for his code review. I still have the image burned in my mind about how happy he was thinking about the required protocols, learning about WSE 2.0 (for WS security), implementing the WS itself to be extendable and maintainable, that it was almost tragic to see his face after I’ve told him that we should write an extensive unit tests for this WS due to his importance and it’s extensive usage by our 3rd party softwares. The WebService had 5 methods and it handled 18 tables in our application so I thought about testing 3 different cases for each method, for each table:



  1. 1 valid xml which should return the expected valid data from the WS.
  2. invalid structure xml which should return an error from the WS.
  3. 1 invalid data xml which should return an error from the WS.

The math was simple:


3 xml files * 5 methods * 18 tables = 270 unit tests !


Eran started to plan the following 2 months for this taks but I had something else in mind – let’s generate those unit test and hack, while we’re in the middle of it, let’s generate the XMLs as well !


I started writing the templates(while Eran was looking and learning the required basics) via CodeSmith (did I mentioned that this tool rocks? I’m sure I did, but again – Eric, great job) and after 2 hours we had all the xml files generated. Eran continued the job and written the classes (which holds the unit test methods) and checked that everything integrated properly. After a total of about 6 hours we had 270 unit tests, but more important, we managed to avoid a lot of dirty work and in case we need to support a new table, the unit tests for it will be only 2 clicks job; This is quite extendable, doesn’t it ?