Class DBUnitTestExecutionListener

java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
org.flywaydb.test.dbunit.DBUnitTestExecutionListener
All Implemented Interfaces:
org.springframework.core.Ordered, org.springframework.test.context.TestExecutionListener

public class DBUnitTestExecutionListener extends org.springframework.test.context.support.AbstractTestExecutionListener implements org.springframework.test.context.TestExecutionListener
A TestExecutionListeners to get the annotation DBUnitSupport up and running.

Attention: at the moment this implementation is only tested with H2 and Oracle!

The annotation DBUnitSupport Important if the annotation FlywayTest are used the system properties for jdbc.url, jdbc.driver, jdbc.username, and jdbc.password should be set.

Also the test application context should contains code like this:
 
 	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 		<property name="location"><value>jdbc.properties</value></property>
 		<property name="ignoreResourceNotFound" value="true"/>
 	</bean>

     <!-- H2 Setup
     -Djdbc.driver=org.h2.Driver
 	-Djdbc.url=jdbc:h2:./db/testCaseDb
 	-Djdbc.username=OC_MORE_TEST
 	-Djdbc.password=OC_MORE_TEST

      -->
 	<bean id="dataSourceId" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 		<property name="driverClassName"><value>$jdbc.driver</value></property>
 		<property name="url"><value>$jdbc.url</value></property>
 		<property name="username"><value>$jdbc.username</value></property>
 		<property name="password"><value>$jdbc.password</value></property>
 	</bean>
 
 

If this setup is used exist the possibility to run test again different database such as H2 or Oracle.

Usage inside the TestClass

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = { "/context/simple_applicationContext.xml" })
 @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
                DBUnitTestExecutionListener.class })
 public class SimpleLoadTest {

        @Test
        @DBUnitSupport(saveFileAfterRun = "./dbunit/result/oneMandatorCreate.xml", saveTableAfterRun = {
                "MANDATOR", "" })
        public void oneMandatorCreate() throws SQLException
        {
                ...
        }

        @Test
        @FlywayTest
        @DBUnitSupport(loadFilesForRun={"INSERT","dbunit/insert/oneMandatorInsert.xml"})
        public void oneMandatorInsert() throws SQLException
        {
                ....
        }
 }
 
If the DefaultDatabaseConnectionFactory do not fit for usage with DBunit it can be customized.
Define a bean in the application context that implement the interface DatabaseConnectionFactory. Afterwards the own implementation of the database connection factory will be used for generating database connection.

Version:
1.7
Author:
Florian Eska
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    protected final org.apache.commons.logging.Log
     

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    Allocates new AbstractDbSpringContextTests instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterTestClass(org.springframework.test.context.TestContext testContext)
     
    void
    afterTestMethod(org.springframework.test.context.TestContext testContext)
    Only annotation with saveIt will be executed
    void
    beforeTestClass(org.springframework.test.context.TestContext testContext)
    Only annotation with loadFiles will be executed
    void
    beforeTestMethod(org.springframework.test.context.TestContext testContext)
    Only annotation with loadFiles will be executed
    protected org.dbunit.database.IDatabaseConnection
    getConnection(DataSource dataSource, org.springframework.test.context.TestContext context)
    Get the dbunit specific database connection
    int
     
    void
    prepareTestInstance(org.springframework.test.context.TestContext testContext)
     
    void
    setOrder(int order)
    change the default order value;

    Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener

    afterTestExecution, beforeTestExecution

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.test.context.TestExecutionListener

    afterTestExecution, beforeTestExecution
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
    • dbConnectionFactory

      @Autowired(required=false) protected DatabaseConnectionFactory dbConnectionFactory
  • Constructor Details

    • DBUnitTestExecutionListener

      public DBUnitTestExecutionListener()
      Allocates new AbstractDbSpringContextTests instance.
  • Method Details

    • beforeTestClass

      public void beforeTestClass(org.springframework.test.context.TestContext testContext) throws Exception
      Only annotation with loadFiles will be executed
      Specified by:
      beforeTestClass in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      beforeTestClass in class org.springframework.test.context.support.AbstractTestExecutionListener
      Throws:
      Exception
    • prepareTestInstance

      public void prepareTestInstance(org.springframework.test.context.TestContext testContext) throws Exception
      Specified by:
      prepareTestInstance in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      prepareTestInstance in class org.springframework.test.context.support.AbstractTestExecutionListener
      Throws:
      Exception
    • beforeTestMethod

      public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception
      Only annotation with loadFiles will be executed
      Specified by:
      beforeTestMethod in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      beforeTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListener
      Throws:
      Exception
    • afterTestMethod

      public void afterTestMethod(org.springframework.test.context.TestContext testContext) throws Exception
      Only annotation with saveIt will be executed
      Specified by:
      afterTestMethod in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      afterTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListener
      Throws:
      Exception
    • afterTestClass

      public void afterTestClass(org.springframework.test.context.TestContext testContext) throws Exception
      Specified by:
      afterTestClass in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      afterTestClass in class org.springframework.test.context.support.AbstractTestExecutionListener
      Throws:
      Exception
    • getConnection

      protected org.dbunit.database.IDatabaseConnection getConnection(DataSource dataSource, org.springframework.test.context.TestContext context) throws Exception
      Get the dbunit specific database connection
      Parameters:
      dataSource -
      Returns:
      Throws:
      Exception
    • setOrder

      public void setOrder(int order)
      change the default order value;
      Since:
      3.2.1.1
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
      Overrides:
      getOrder in class org.springframework.test.context.support.AbstractTestExecutionListener
      Returns:
      order default 4500
      Since:
      3.2.1.1