Skip to content

spring dbunit test module

Stephane Landelle edited this page Mar 25, 2014 · 9 revisions

Spring Test support.

It is quite similar to Unitils but :

  • it sticks to standard spring-test syntax : no special class to inherit, no @SpringBean...
  • it won't impose anything on you : no EasyMock for Mockito fans!

How?

Quick start:

	@RunWith(SpringJUnit4ClassRunner.class)
	@ContextConfiguration("applicationContext.xml")
	@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
                                  DataSetTestExecutionListener.class })
	@DataSet
	public class MyTest {

		@Autowired
		private IDao myDaoUnderTest;

		@Test
		public void testSomethingWithDao() {
			myDaoUnderTest.doSomething();
			...
		}

		@Test
		public void testSomethingElseWithDao() {
			// data has been reloaded and won't conflict with what's been done in testSomethingWithDao
			myDaoUnderTest.doSomethingElse();
			...
		}
	}

Easy, isn't it? Well, that's the minimal configuration. In that case, the engine will try to use a DataSet file named after your test class and located in the same package, and load it with a HSQLDB dialect.

Configuration customization:

Configuration can be tweaked thanks to @DataSet attributes :

  • value/locations : DataSet files locations.
    • Default is one file named dataSet.xml and located in the same package as the test Class.
    • A plain path, e.g. "myDataSet.xml", will be treated as a classpath resource from the same package in which the test Class is defined.
    • A path starting with a slash is treated as a fully qualified class path location, e.g.: "/com/example/whatever/foo.xml".
    • A path which references a URL (e.g., a path prefixed with classpath:, file:, http:, etc.) will be added to the results unchanged.
  • setUpOperation : Operation performed on "Before" phase (default CLEAN_INSERT)
  • tearDownOperation : Operation performed on "After" phase (default NONE)
  • format : DataSet file format (default FLAT)
  • dbType : DataBase type (default HSQLDB)
  • dataSourceSpringName : name of the DataSource spring bean name to be used to connect to the DataBase, in case of multiple choices in the TestContext
  • columnSensing
  • dtdLocation
  • dtdMetadata
  • caseSensitiveTableNames
  • escapePattern
  • batchSize
  • fetchSize
  • qualifiedTableNames
  • batchedStatements
  • skipOracleRecycleBinTables
  • tableType
  • schema

Advanced features

Multiple DataSets support

One can specify multiple DataSet locations. In this case, corresponding DataSets will be played in reverse order during teardown phase so that they'll behave nicely if they depend on each other.

Multiple setup and teardown operations support

One can specify multiple setup and teardown DBOperations that will be transformed to a CompositeOperation, just like the same way CLEAN_INSERT is a composite of DELETE_ALL and INSERT.

Transactional/Rollbacking support (contributed by Patrice CAVEZZAN)

One can use spring-test built-in transactional/rollbacking support: just switch from DataSetTestExecutionListenerto RollbackTransactionalDataSetTestExecutionListener:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "applicationContext-test-transactional.xml", "applicationContext-test-hsqldb.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
                          TransactionalTestExecutionListener.class,
                          RollbackTransactionalDataSetTestExecutionListener.class })
@TransactionConfiguration
@Transactional
@DataSet
public class MyEntityDaoHSQLDBRollbackTransactionalTest {

}

Expected DataSet (contributed by Régis POUILLER)

One can validate the database state after the test against a given dataset:

@ExpectedDataSet("expectedData.xml")