Easy unit testing with Spring and DBUnit
How To
@ContextConfiguration(locations = {"/spring-tests.xml" })
@DbUnitExecution(datasets = {"/db1-load.xml" })
public class SampleDbUnitTest extends AbstractDbUnitTestNGSpringContextTests
{
@Test
public void testSomething() {
...
}
}You can setup also more than one execution if your tests depend on more than one database or schema, annotating your class like in this example:
@DbUnitConfiguration(dbUnitExecutions = {
@DbUnitExecution(datasets = {"/db1-load.xml" }, dataSource = "dataSource1"),
@DbUnitExecution(datasets = {"/db2-load.xml" }, dataSource = "dataSource2") })
Note that this test classes don't extend Spring transactional tests, so you need to manually turn on Transactions using standard Spring annotations:
@TestExecutionListeners({TransactionalTestExecutionListener.class })
@Transactional
Also note that the content of your db will definitively be modified by these tests, they are expected to be run on a test-only instance!
This is a test runs manually Spring configuration and transactional test with Openutils testing annotaion, configuring Hsql instead of DbUnit as:
- loading dataset from file db1-load.xml
- using HsqlDataTypeFactory as data type factory
- using configured data source named "dataSource" and configured in spring-test.xml
- using schema name "SCHEMA_NAME" in SQL statement
- using qualified table names in SQL statement
@ContextConfiguration(locations = {"/spring-test.xml" })
@TestExecutionListeners(value = TransactionalTestExecutionListener.class)
@DbUnitConfiguration(
dbUnitExecutions = {
@DbUnitExecution(
datasets = {"/db1-load.xml" },
dataTypeFactory = HsqldbDataTypeFactory.class,
dataSource = "dataSource",
schema = "SCHEMA_NAME",
features = {
@DbUnitFeature(
key = DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES,
value = true)
}
)
}
)Base test class support dependencies injection so you can use library also for testing your DAOs and managers.
This is a sample test that can be used to test a DAO:
public class PersonalDataDAOTest extends AbstractDbUnitTestNGSpringContextTests
{
@Autowired
private PersonalDataDAO personalDataDAO;
@Test
public void testFindAll()
{
List<PersonalData> all = personalDataDAO.findAll();
assert all.size() == 3 : "Problem during search: " + all.size();
}
}If you need or prefer, you can also write your by your own the code for retrieving instances of you object from the apllication context.
This is an sample test that can be used to test a manager:
public class PersonalDataManagerTest extends AbstractDbUnitTestNGSpringContextTests
{
@Test
public void testSomething()
{
PersonalDataManager personalDataManager = (PersonalDataManager) applicationContext.getBean("personalDataManager");
//test something on manager
}
}Project info & quick links
-
Latest release: 2.2.2
License: LGPL v2.1 - Download Sourceforge file download area
- Bugs and feature requests OpenmindLAB Jira
- Changelog See what's new in release 2.2.2
- Source code Subversion public url
- Javadocs Documentation for the release 2.2.2
- Maven The dependency snippet for your pom