Definition of what Unit Tests are
The Artima.com website has an article outlining A Set of Unit Testing Rules. In this article, the author, Michael Feathers, outlines the following:
A test is not a unit test if:
- It talks to the database
- It communicates across the network
- It touches the file system
- It can’t run at the same time as any of your other unit tests
- You have to do special things to your environment (such as editing config files) to run it.
For the most part, I completely agree with this list. I have seen, at my last job, the problems that you can run into when you try to do unit testing where the unit tests, or the code that you are trying to unit test, needs to interact with the filesystem or a database.
However, these parts of the code still need to be tested. So the question that remains is how to test code that interacts with a DB, a filesystem, or a session bean? I guess one of the easiest solutions is to use dependency injection and replace the real external systems with mock objects. This allows you to test your methods in isolation from the real database, filesystem, or app server. Unfortunately, I left my last job before I had a chance to test this idea.