In my project, I’ve used spring, jpa with PostgreSQL DB,
I’ve lots of table in DB and I need to have Unit testing of all of them.
Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.
Any suggestion for this?
I’ve some idea of DBUnit but in that I need to write .xml files for every input data for every test and need to insert data in setup() and clear/remove data in tearDown(), but doesn’t seems better strategy to me.
Any suggestion is appreciated.
Thanks.
From my other answer posted earlier in the day, yes, this is possible using DbUnit. (Based on your edit, you don’t need this; the subsequent section of my answer addresses why I use DbUnit, and when I wouldn’t use it).
The following code snippet demonstrates how the setup of every test is performed:
The
database-test-setup.xmlfile contains the data that will be inserted into the database for every test. The use ofDatabaseOperation.CLEAN_INSERTin thesetupmethod ensures that all the tables specified in the file will be cleared (by a delete of all rows) followed by an insert of the specified data in the test data file.Avoiding DbUnit
I use the above approach specifically to clear out sequences before the start of every test, as the application uses a JPA provider which updates the sequences in a separate transaction. If your application is not doing anything like that, then you can afford to simply start a transaction in your
setup()method and issue a rollback on teardown after the test. If my application didn’t use sequences (and if I didn’t desire to reset them), then my setup routine would have been as simple as:Also, I use dbdeploy with Maven, but that is primarily for keeping the test database up to date with the versioned data model.