The following is a suggestion from Craig Russell that would allow test clean up
to happen in a much more intuitive way:
It might be worthwhile to look at adding some behavior to the toplink
base class JUnitTestCase to allow a subclass to acquire an
EntityManager that will be cleaned up by the tearDown method in the
Maybe add a protected field EntityManager em to the base class.
Subclass tests could either clean up their own EntityManager using a
try finally pattern or if they assigned the base class em field could
simply throw an exception and exit. At the end of the test, if the em
field is not null, the base class tearDown would clean it up.
This pattern provides for cleaning up the EntityManager without
duplicating or adding code to each test case.
From my experience, it's difficult to look at a test case that does
its own cleanup, because each test writer's ideas of what needs to be
cleaned up tends to be different. Some tests use a try catch where
the catch cleans up, some don't clean up at all, some use try finally.
It's more efficient to have common code that can evolve as our
understanding of how to clean up evolves.