Thursday, 7 August 2008

Work progresses

Just when you thought you were meant to be testing... you find yourself developing more changes to your code!  Ever find yourself doing this?

This must be the main reason why it is common practice to get someone else to test your code. 

Is this a form of agile programming?

(Hoping to justify my actions here)

Either way; I'm really enjoying the combination of unit testing with mock classes.

I'm not using a standard mocking framework though, I've found the easiest way to remove dependences has been to use the following steps:

  1. Define Interfaces: This is probably a very obvious step, but I'm pretty new to unit testing so I haven't had any real need to create any before.
  2. Use a Simple Factory: Use a Simple factory class to create concrete instances of the interface.  This allows you to swap the concrete implementation for a mock version.
  3. Create a Mock Version: Control of the mock version is supplied by static variables inside it.  Because they are static you can reach in from inside the unit test and determine the mock classes responses.

It is then a simple matter to build unit tests which can ensure 100% code coverage. 

If you were anything like me and were unsure of unit testing.  Don't avoid it because of the overly complex mocking frameworks.  With a little interface magic you can easily create your own mock class implementations.

No comments: