How to load DBUnit test data once in each case using Spring Test

Spring Test helps reset any changes made to the database as part of the test method. This means that there is no need to spend time deleting / reloading test data before each testing method.

But if you use the @BeforeClass Junit annotation, then this forces the data loader to be static. The question that is addressed here is: Why is the jUnit's fixtureSetup attribute static?

If the data initialization method is static, then there should be data connection methods and a data source ... and then ... force everything to be static ... that will not work. At what point I ask: what is the use of Spring? The ability to check rollback when you need to delete / reload test data in any case for each test?!?!

+12
spring-test junit dbunit
Jun 09 2018-10-06
source share
4 answers

One approach that works is to create a "data initializer" class, add it to the test context of the Spring application, which also has your data source, and associate this application context with your tests. It depends on the fact that Spring caches the application context between test calls.

For example, a test superclass:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:test-application-context.xml"}) @Transactional public abstract class DataLoadingTest { @Autowired protected DatabaseInitialiser databaseInitialiser; } 

With test-application-context.xml :

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" .../> <bean class="DatabaseInitialiser"> <property name="dataSource" ref="dataSource"/> </bean> </beans> 

and

 public class DatabaseInitialiser extends JdbcDaoSupport { @PostConstruct public void load() { // Initialise your database here: create schema, use DBUnit to load data, etc. } } 

In this example:

  • all database-based tests are extended by DataLoadingTest ;
  • Spring initializes the application context on the first call of the call;
  • this calls DatabaseInitialiser.load() via the @PostConstruct annotation;
  • Spring maintains the application context in the cache;
  • additionally check the call wire in the DatabaseInitialiser from the application context, which is already cached;
  • tests are transactional and roll back at the end to the original dataset.

Similarly, DatabaseInitialiser can have an annotated @PostDestroy method to perform any rollback required at the end of the entire test run.

+14
Jan 25 2018-12-12T00:
source share

Spring Test and DbUnit are two great frameworks. But to combine them does not make sense. Because Spring Test rolls back over the connection, it cleans up after that, and DbUnit cleans up and inserts the test data into the @Before method.

Use Spring if you are not dependent on any dynamic data and dbUnit otherwise.

+1
Jun 10 2018-10-10
source share

We use DBUnit in conjunction with Spring Test extensively. But we do not use the DBUnit function to delete data at the end of the test.

We put a bunch of DBUnit attachments for our test data in the @Before method to initialize the test. Then, when the test is complete, we let the Spring rollback function return the database to its original state.

The biggest problem we are dealing with is that DBUnit data must be loaded before each test, which can be a major performance hit. Most of our tests using DBUnit are read only, checking the behavior of the application based on certain predefined behavior. Thus, we have the habit of creating master tests, which then run all the small grain tests in the batch as part of the same transaction.

+1
Jun 28 '11 at 9:19
source share

The methods are annotated using @BeforeTransaction , as the name implies, before the start of each test transaction. If in this method you can determine if the test data is loaded, then you can download the data if necessary.

Remember that the data remains in your (in memory) database for all subsequent tests.

We use this to load β€œstatic” data, which in the production environment will also be loaded into our database when it starts. Thus, we actually use exactly the same code and data for our tests, and do not rely on export (DbUnit), which may be outdated.

0
Jan 31 '13 at 11:49
source share



All Articles