Why it’s important to be able to unit test outside the container

Daniel Steinberg has a “note on Java.Net”:http://weblogs.java.net/pub/wlg/1732 about testing EJBs out of the container, and wonders why people make a fuss about it. There’s a simple reason: speed.

I run my unit tests a lot; I would run localised subsets of the tests every couple of minutes, and the full test suite at least once an hour if not more[1]. Running inside a container isn’t slow, but getting the code there sure is, by comparison.

It’s not hard to write your code so that you have a set of POJO-ish objects which are given services provided by the container. This makes unit testing reasonably straight-forward. Your integration tests, then, must ensure that you correctly set up the services for your POJO objects (and yes, you need integration tests). Obviously, integration tests need to run in-container.

This also means you don’t unit test everything outside of the container; it is obviously rather pointless to do a lot of unit testing around the glue code (just enough so that you have a reasonable degree of confidence with it).

One of the nice things about using IoC/dependency injection as a style of coding is that you can easily set up your EJBs or Servlets to inject the dependencies for you into your POJO classes.


fn1. One of the standard hints about good typing is to take a break every hour; I hit the full-build button just before having the break.

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s