IoC and Unit Testing

1 AM
December 6, 2004

We’re using Spring at work, and getting major benefits in our unit testing. We are testing more code, and more thoroughly than on any Java project I’ve been involved with before now. I was going to write something about how Inversion of Control frameworks like Spring provide these benefits, but then I found this:

Large applications can prove troublesome to unit test exhaustively, especially if there is any kind of tight coupling between components. Such coupling of components can make it difficult to test them separately.

Needle [an Inversion of Control framework for Ruby] ... encourages loose coupling of components. Also, because dependencies are never instantiated in code, but are instead accepted via setters or constructor arguments, it is trivial to replace those dependencies with mock objects at unit test time.

(That’s from the Needle Manual. Needle is an Inversion of Control framework for Ruby.)