When I test DAO module in JUnit, an obvious problem is: how to recover testing data in database?
For instance, a record should be deleted in both test methods testA() and testB(), that means precondition of both test methods need an existing record to be deleted. Then my strategy is inserting the record in setUp() method to recover data.
What’s your better solution? Or your practical idea in such case? Thanks
A simple solution is to roll back the transaction after the test (for example in
tearDown()). That way, the tests can make all the changes they like but they won’t change the database (don’t forget to turnautoCommitoff for the connection).There is a drawback, though: If a test fails, you can’t peek at the database to figure out why. Therefore, most of my tests clean the database before they run and they use autoCommit so I can see the last state where it failed, run a fixed SQL query against the data, etc.