I’m writing some integration tests for my application in Spring.I want to test some services however they may call some Data Access Objects and new data will be saved in my DB.I have two alternatives to clear every thing in database after testing is complete:
- Use a temp Database to insert test data there
- clear all my tables everytime I do a test
however i’m looking for let’s say a clean and forward way, so that I can use my database without manual cleaning. Any ideas?
If you are using Spring framework then I think you should have already checked Spring’s reference on how to do testing.
You’ll see that Spring will make it so simple for you, in a way that you don’t need to interact with transactions directly.Adding a @Transactionl annotation at top of your tests is all you need to do, So what’s the benefit of making a test transactional? yes! As mentioned earlier by other answers it will let you rollback your transactions so nothing will remain in your DB. Take a look at this sample code:
Something important to note is that your database must be InnoDB, So if you are using mySQL which is myISAM by default consider altering your tables beforehand.