Junit tests fails randomized while using database and entity manager
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?
- For each test method I create a new in memory database with a different name.
- I create a new entitymanagarfactory and entitymanagar instance.
- I've disabled second level caching.
- I create all table via script (no error occurs so database is really fresh).
- I do some db actions and test.
- I clear session and em.
- I d开发者_开发知识库rop all object in my in-memory database
- I shut down the database
- I close em and emf.
Have I to do more?
Thanks a lot...
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.
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.
精彩评论