views:

63

answers:

2

I try to write an big test class.

I'm using Junit, Hibernate and TopLink and H2 database. Before this I used EJB3Unit (including Hibernate and H2).

My test class has 57 test methods. If I run all test at once randomized one or more test fails. If I run each test alone, I get no error.

Has anyone an idea what's going wrong? And how I can prevent this?

  1. For each test method I create a new in memory database with a different name.
  2. I create a new entitymanagarfactory and entitymanagar instance.
  3. I've disabled second level caching.
  4. I create all table via script (no error occurs so database is really fresh).
  5. I do some db actions and test.
  6. I clear session and em.
  7. I drop all object in my in-memory database
  8. I shut down the database
  9. I close em and emf.

Have I to do more?

Thanks a lot...

+1  A: 

It seems that there is dependency among the tests. ideally you should restore the database to its original state after each test by using a tearDown method (in JUnit 4, use the @After annotation).

If you're already doing that then the dependency is more subtle. To find out its cause I suggest you start doing a binary search on the tests: comment out half of your tests. If the random failure persists then comment out half of the remaining half (and so on). If the failure disappears then the problem is in the other half: uncomment and comment out the other half. This process will converge quite quickly.

Good hunting.

Itay
I use @After and @Before for setting up a new database and killing it.Sometime all test pass. All testing with deletion test method and so on let me think, that I need only a "critcal" mass of test methods to get more errors or less.
marabol
A: 

Dependencies are a possibility for this random failing.

An other reason can be the order of elements in a collection. Once i was writing a test and depended on the first element. It was not sorted so i was not sure that the object i was asking was always the same.

Michael Bavin
Thanks a lot... The ordering is not the problem in this case (but I did this, too ;-) ). It seams, there are "phantom" entries. I think that the PM save some information in local thread or static variables. If I've more time, I'll look deeper.
marabol