Chapter 8. Troubleshooting JUnit

 

This chapter covers

  • Problems finding your tests
  • Problems executing your custom test suite and test setup
  • Problems executing your test after the first assertion fails
  • Problems reloading classes between tests
  • Problems reloading XML-related classes
  • Problems narrowing EJB references in your tests

Wouldn’t it be nice if everything worked exactly the way you expected it to work? Come to think of it, if that were so then you wouldn’t be reading this book—or maybe because you’re reading this book, the software you build will work exactly the way you expect. At least, that’s the idea.

This chapter is about how to handle those situations in which JUnit doesn’t do what you expect. Although JUnit is simple, it is a framework, and when you use a framework you always have a learning curve. More than that, the JUnit community pushes the boundaries of what JUnit can do every day. Someone, somewhere is using JUnit to do something it has never done before. Putting software through stress can unearth all sorts of problems, and given the number of people using JUnit these days, it is under considerable stress.

The most common problem

A problem with the CLASSPATH environment variable

8.1. JUnit cannot find your tests

8.2. JUnit does not execute your custom test suite

8.3. JUnit does not set up your test fixture

8.4. Test setup fails after overriding runTest()

8.5. Your test stops after the first assertion fails

8.6. The graphical test runner does not load your classes properly

8.7. JUnit fails when your test case uses JAXP

8.8. JUnit fails when narrowing an EJB reference