This library greatly simplifies writing integration tests for projects employing MyBatis ORM framework and Guice dependency injection framework. It also makes DbUnit compatible with JUnit 4.
<dependency>
<groupId>org.plukh</groupId>
<artifactId>dbunit-guice-mybatis</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
public class MyBatisDAOModule extends XMLMyBatisModule {
public MyBatisDAOModule() {
}
@Override
protected void initialize() {
//Set environment
setEnvironmentId("test");
//Bind DAO implementations
bind(TopicDAO.class).to(MyBatisTopicDAOImpl.class);
}
}
- Extend
DbUnitTest
class. - Add
@GuiceModules(<your module classes here>)
- note that all modules have to have public no-args constructors. - Optionally, add
@DataSets
annotation (either on the class itself or on individual test methods); setoverride
totrue
for method datasets to completely replace class datasets (by default, they're combined together).DbUnitTest
expects standardXmlDataSet
dataset files - see library tests for some examples. - See
DbUnitTestTest
for examples on how to assert database state after update;TestUtils
contains helpful methods to assert correctness of ordered and unordered collections returned by your DAO methods.
If test class is annotated with @RequestStaticInjection
, its static members will be injected by Guice when injector is being created. This should be used with care, as it breaks isolation between tests - but is sometimes unavoidable (if you'd like to migrate/clean your database with Flyway before each test class, for example).
This specific implementation of DbUnitTest
is targeted towards MySQL. To make it work with other databases, override DbUnitTest#setUpDatabaseConfig()
, DbUnitTest#getSetUpOperation()
and DbUnitTest#getTearDownOperation()
, adding specific operations required by your database (like, you may want to implement ALTER TABLE ... DISABLE TRIGGER
for disabling foreign key checks in PostgreSQL, as it doesn't support global check on/off flag).