I’m writing some integrations tests in JUnit. What happens here is that when i run all the tests together in a row (and not separately), the data persisted in the database always changes and the tests find unexpected data (inserted by the previous test) during their execution.
I was thinking to use DbUnit, but i wonder if it resets the auto-increment index between each execution or not (because the tests also check the IDs of the persisted entities).
Thanks
M.
It’s a best practice to put your database in a known state before a test execution and DBUnit provides everything required for that. But don’t rely on auto incremented columns, put them also in your DBUnit dataset. Pros: you can manually verify the database state after executing a test that fails. Cons: you need to setup and to maintain datasets.
The other approach is to run each test method inside a transaction (and to rollback the transaction at the end of the execution). Pros: data are easier to setup and maintain (in the databse). Cons: Fixing a failed test is less convenient.