Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and clean DB setup only once for testing all DAOs [closed]

Does anybody knows a way to achieve the following flow for testing DAOs:

  • Run SQL script to create common DB setup (insert data into all tables)
  • Test DAO1
  • Test DAO2
  • Clean-up DB Data created in Step 1

Using Spring, Hibernate, JUnit, Maven stack.

I understand that best practice would be that we create data for each test DAO (@BeforeClass) and clean-up the same after all tests are done (@AfterClass).

But in our case there are just too many dependencies between different database tables (client’s legacy DB :-( can’t do anything about that at the moment). Populating each table with test data requires data in many other tables as well. So, creating data individually for each DAO would be very tough and time consuming. So, we really need to create DB test data only once.

I’ve created test data using static block in BaseDAO (extended by each DAO Test Class) – which obviously runs only once. But problem how to clean-up the same when all tests (of all DAO Test subclasses) get completed. An @AfterClass teardown method in base class will run each time after a DAO Test class completes.

Please advise.

like image 961
haps10 Avatar asked Sep 27 '11 07:09

haps10


1 Answers

If you're using Maven, a good option is to use DBUnit. It lets you export test data from the database (or just write it in XML), to be imported for your tests. It has a Maven plugin which will do what you require.

like image 117
Daniel Avatar answered Sep 22 '22 21:09

Daniel