Integrating DbUnit With Spring Test Framework
After we have finished this lesson, we
- Know what Spring Test DbUnit is.
- Can get the required dependencies with Maven and Gradle.
- Know how we can integrate DbUnit with Spring Test framework.
This lesson assumes that:
What Is Spring Test DbUnit?
Spring Test DbUnit provides integration between DbUnit and Spring Test framework. It has three key features:
- We can initialize our database into a known state by using the
@DatabaseSetupannotation. - We can write assertions for the content of our database by using the
@ExpectedDatabaseannotation. - We can specify advanced DbUnit configuration options by using the
@DbUnitConfigurationannotation.
Let’s move on and find out how we can get the required dependencies with Maven and Gradle.
Getting the Required Dependencies
Before we can integrate DbUnit with Spring Test framework, we have to declare the DbUnit and Spring Test DbUnit dependencies in our build script.
If we are using Maven, we have to add the following snippet into our pom.xml file:
1<dependency>
2 <groupId>org.dbunit</groupId>
3 <artifactId>dbunit</artifactId>
4 <version>2.5.3</version>
5 <scope>test</scope>
6</dependency>
7<dependency>
8 <groupId>com.github.springtestdbunit</groupId>
9 <artifactId>spring-test-dbunit</artifactId>
10 <version>1.3.0</version>
11 <scope>test</scope>
12</dependency>
If we are using Gradle, we have to add the following snippet into our build.gradle file:
1integrationTestCompile(
2 'org.dbunit:dbunit:2.5.3',
3 'com.github.springtestdbunit:spring-test-dbunit:1.3.0'
4)
We can now configure Spring Test DbUnit. Let’s find out how we can do it.
Configuring Spring Test DbUnit
We have to configure the test execution listeners that react to the test execution events that are published by the Spring Test framework. Typically we don’t have worry about this because Spring Test framework registers the default listeners for us.
However, because we need to use Spring Test DbUnit, we have to register a new test execution listener called: DbUnitTestExecutionListener. Unfortunately, there is no way to register additional test execution listeners. If we want to register an additional test execution listener, we have to configure all registered test execution listeners.
We can configure the used test execution listeners by annotating our integration test class with the @TestExecutionListeners annotation. Our next step is to configure the registered test execution listeners. The minimum setup that is useful, if we are writing integration tests for Spring powered repositories or services, contains the following test execution listeners:
- The
DependencyInjectionTestExecutionListenerprovides support for dependency injection and initialization of test instances. - The
TransactionalTestExecutionListenerprovides support for test-managed transactions that honor the@Transactionalannotation. - The
DbUnitTestExecutionListenerprovides support for the features provided by the Spring Test DbUnit library.
After we have configured the required test execution listeners, the source code of our test class looks as follows:
1import com.github.springtestdbunit.DbUnitTestExecutionListener;
2import org.junit.experimental.categories.Category;
3import org.junit.runner.RunWith;
4import org.springframework.test.context.ContextConfiguration;
5import org.springframework.test.context.TestExecutionListeners;
6import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
8import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
9
10@RunWith(SpringJUnit4ClassRunner.class)
11@ContextConfiguration(classes = {PersistenceContext.class})
12@TestExecutionListeners({
13 DependencyInjectionTestExecutionListener.class,
14 TransactionalTestExecutionListener.class,
15 DbUnitTestExecutionListener.class
16})
17@Category(IntegrationTest.class)
18public class FindMessageByIdTest {
19
20}
I haven’t personally noticed any problems with transactions when I have written integration tests that use this configuration. However, if you notice any problems, you should use the TransactionDbUnitTestExecutionListener instead of the DbUnitTestExecutionListener.
Also, keep in mind that this lesson uses the minimum configuration. In other words, you might need to register other test execution listeners as well. That is why I recommend that you take a look at the following resources:
Additional Reading:
We have now enabled Spring Test DbUnit. If we use the default configuration, DbUnit:
- Obtains the required database connections by using the bean whose name is either dataSource or dbUnitDatabaseConnection.
- Loads the used data sets by using the
FlatXmlDataSetLoaderclass.
This is a good start, but sometimes we want to use a custom DbUnit configuration. We will talk more about this in the next lesson of this topic.
Let’s summarize what we learned from this lesson.
Summary
This lesson has taught us three things:
- Spring Test DbUnit provides integration between DbUnit and Spring Test framework.
- Before we can integrate DbUnit with Spring Test framework, we have to declare the DbUnit and Spring Test DbUnit dependencies in our build script.
- We can enable Spring Test DbUnit by registering a new test execution listener called:
DbUnitTestExecutionListener