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.

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 )

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