Monday 12 March 2007

Unit Testing in Java

I'm sold on JUnit testing. I've been using it for 3 or 4 years now and it has proved invaluable in that time.

New techniques (at least to me) keep popping up all the time. Originally I setup a unit test database with lots of static tables. I had a fixed dataset that didn't need to change.

And then other developers joined the project...

And they started writing to those tables as part of their tests...

And changing schemas....

Suddenly we couldn't run tests simultaneously (no transactions were used - doh). We couldn't test branches of old code (because schemas had changed).

The solution: twofold.

The first one was to use HSQLDB together with inmemory tables. This works really well but you end up having code to create SQL tables duplicated in the actual database, a DDL file and in the unit test code. Not an enormous problem because if the tables change - the tests fail!

The second one was to use interfaces more, if the table was being used to provide static we just create an interface on the cache and implement that interface in the unit test. This works very well indeed.

Another solution, suggested by a new team member, is to use Spring. This has some nifty little classes to help with testing. More soon....

Sunday 11 March 2007

The Old New Thing

This book is based upon the popular blog by Raymond Chen, a longtime developer on the Microsoft Windows team.

I've read quite a few Windows Development books in my time, I first started using Windows at the 1.0 release.

Over the years books have focused on what you can and what you can't do. Sometimes the focus is on what you shouldn't be able to do but can do (Undocumented Windows by Andrew Schulman).

This is one of the first books I can remember reading that explains why. Why do strange things happen in Windows?

There are chapters about compatibility, hardware reminiscences, security etc.

As a software professional developer working in a corporation it's easy to lose site of the wider world. I target a fixed client base using the same machines and platform. Microsoft have to cope with strange hardware, buggy third party drivers, developers too clever for their own good etc etc.

So, whilst not a Microsoft apologist (these days I use Java 80% of the time) I came to have a new respect for the Windows team.

I can certainly understand how the compromises they had to make came about: I used to work for an independent software house and we had to fit very large programs into small amount of RAM. We used all sort of tricks, self modifying code, portions rewritten in assembler, custom loaders, OS hacks.

At least I could remove these later on when machines got better, Microsoft still have to support programs that use these sort of tricks. How they do it is nothing short of amazing.