Archive for December, 2009

Unit testing and the big dependency graph

Friday, December 25th, 2009

So you are working on a BIG project and you discover that writing unit-tests is really a pain. Why? Because once you want to create a new test, you find out that the class you want to test has got a lot of dependencies to other classes, which you do not want to be bothered with right now.

If you ever encounter this problem, you have actually encountered a big code-smell. Code smells are bad, very bad, because it means the whole implementation of the system and maybe even the whole system design is bad. And if you cannot easily write  unit-test for the code-base than your code-base will be legacy code in no time.

There is a lot of self reference in the above paragraphs by the way, because legacy code, by the name of it, is code that does not (yet) have unit tests. And if you want to create unit tests (real unit tests: against just a tiny bit of code) as opposed to integration tests (which are written against alot of classes, and undoubtedly will run much slower) you really need a loosely coupled design.

Instead of writing a whole new blog about this, let me share some links I found about this on the ‘net.

First of all I want you to have a look at Singletons are pathological liars, because that is one of the challenges we currently have in our own code base as all our repositories are written as singletons. But better, the author of that article, Misko Hevery, has since he wrote that article created a bunch of really good presentations for the Google Tech Talks. Have a look at the OO Design for Testability presentation of Misko Hevery.

What the presentation makes very clear is to have your business classes depened on interfaces instead of concrete classes. This is all about a loosely coupled design and the Hollywood Principle (don’t call us, we’ll call you!), which is all very nicely explained in the great book by Head First, Design Patterns. Head first design patterns

Anyway, once you get into this deeper, you will find that there are almost two camps on the net.