How To

To get started with unit testing and Openutils testing you need to know:

All setup for DbUnit can be configured using annotations. Here is a quick overview of the annotation.

This is a sample test that can make use of data loaded by DbUnit:

@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