Create and clear the database setup only once to test all DAOs

Does anyone know a way to achieve the following thread for testing DAO:

  • Run SQL script to create a general database setup (insert data into all tables)
  • DAO1 test
  • DAO2 test
  • ...
  • Cleanup DB Data Created in Step 1

Using Spring, Hibernate, JUnit, Maven stack.

I understand that it would be best practice to create data for each DAO test (@BeforeClass) and clear the same after all tests (@AfterClass).

But in our case there are too many dependencies between different database tables (clients that are outdated DB :-( could not do something about this at the moment). To fill each table with test data, data is needed in many other tables., Creation Separate data for each DAO would be very complex and time consuming, so we really need to create DB test data only once.

Ive created test data using a static block in BaseDAO (extended by each DAO test class), which explicitly only works once. But the problem is how to clear them when all tests (of all subclasses of the DAO Test) are complete. The @AfterClass teardown method in the base class will run every time after the completion of the DAO Test class.

Please inform.

+4
source share
6 answers

If you are using Maven, a good option is to use DBUnit . It allows you to export test data from a database (or simply write it to XML) to import for your tests. It has a Maven plugin that will do what you need.

+3
source

In Spring 3:

<jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:schema.sql"/> <jdbc:script location="classpath:test-data.sql"/> </jdbc:embedded-database> 

Documentation To clear the database after the tests, create a Spring bean that will be selected only during the tests:

 @Service public class DbCleanup { @Resource private DataSource ds; @PreDestroy public cleanUpDb() { //do your cleanup with DB } } 
+1
source

I am using the following solution.

(1) I have a simple interface called Fixture

 package com.obecto.fixtures; public interface Fixture { void prepare(); } 

(2) I am preparing an SQL device to populate an empty database - this can be done either through entities or by executing SQL as follows:

 package com.avaco2.fixtures; import java.util.logging.Logger; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.stereotype.Component; import org.springframework.test.jdbc.SimpleJdbcTestUtils; import com.obecto.fixtures.Fixture; @Component(value="logEventsSQLFixture") public class LogEventsSQLFixture implements Fixture { private static String IMPORT_SQL = "import-LogEvents.sql"; private static Logger LOG = Logger.getLogger(LogEventsSQLFixture.class.getName()); @Autowired private BasicDataSource dataSource; @Override public void prepare() { SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource); Resource sqlScript = new ClassPathResource(IMPORT_SQL); try { SimpleJdbcTestUtils.executeSqlScript(jdbcTemplate, sqlScript, true); } catch (Exception e) { LOG.severe("Cannot import " + IMPORT_SQL); } } } 

(3) Add your lights to the Test and prepare -them classes in the @Before method.

Always use a different database for tests, so you can safely use the create-drop setting for sleep mode. To reload the context before each test method, you can use the following annotation - @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)

+1
source

No one suggested annotating the @Transactional attribute for each test. If the db engine supports it, this should take care of rolling back from the state. See fooobar.com/questions/1373847 / ... for more details.

+1
source

I usually back up the entire db instance (in fact, I work with Oracle and imp / exp are great tools). SQL Server and alikes have a replication method. After you have prepared your data, just export the entire instance and load it before testing.

Once the test is complete, discard the database and recreate. (resetting and loading the entire database using native programs may be faster than expected)

Hello

PS: if you can, just create a db clone on the virtual machine, then save the snapshot of the virtual machine (so that you can restore it later).

0
source

You can also use a database layout tool such as Acolyte ( https://github.com/cchantep/acolyte ), in which case you do not need to clear the data, being sure which data is available in which JDBC for which it is running test, without changing the JDBC code you want to test.

0
source

All Articles