Does anybody knows a way to achieve the following flow for testing DAOs:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With