Class FlywayTestExecutionListener

java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
org.flywaydb.test.FlywayTestExecutionListener
All Implemented Interfaces:
org.springframework.core.Ordered, org.springframework.test.context.TestExecutionListener
Direct Known Subclasses:
FlywayTestExecutionListener

public class FlywayTestExecutionListener extends org.springframework.test.context.support.AbstractTestExecutionListener implements org.springframework.test.context.TestExecutionListener
Spring test execution listener to get annotation FlywayTest up and running

If the annotation FlywayTest used on class level a clean , init , migrate cycle is done during class load.
If the annotation FlywayTest used on test method level a clean , init , migrate cycle is done before test execution.

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="dbc.properties"/>
   <property name="ignoreResourceNotFound" value="true"/>
 </bean>

 <bean id="flyway" class="org.flywaydb.core.Flyway" depends-on="data.source.id">
   <property name="dataSource" ref="data.source.id"/>
   <property name="locations" value="oracle"/>
 </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="dataSourceRef" 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 test class

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

Notes:
  • If you using spring framework version lower than 3.x the annotation FlywayTest wont work at class level.
  • For spring framework version 2.5.6 use simple_applicationContext_spring256.xml as application context example
  • If you using the annotation FlywayTest more than one time in test classes than do not use parallel execution in surefire plugin.
    With this option you will setup your database in more than one thread parallel!

Version:
2011-12-10, 1.7
Author:
Florian, EddĂș MelĂ©ndez
  • Field Summary

    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)
    no implementation for annotation FlywayTest needed.
    void
    afterTestMethod(org.springframework.test.context.TestContext testContext)
    no implementation for annotation FlywayTest needed.
    void
    beforeTestClass(org.springframework.test.context.TestContext testContext)
    Invoke this method before test class will be created.
    void
    beforeTestExecution(org.springframework.test.context.TestContext testContext)
    implementation for annotation FlywayTest for handling with org.testng.annotations.BeforeMethod annotation.
    void
    beforeTestMethod(org.springframework.test.context.TestContext testContext)
    Called from spring before a test method will be invoked.
    protected org.apache.commons.logging.Log
     
    int
     
    void
    prepareTestInstance(org.springframework.test.context.TestContext testContext)
    implementation for annotation FlywayTest for handling with org.junit.Before or BeforeEach annotation.
    void
    setOrder(int order)
    change the default order value;

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

    afterTestExecution

    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
  • Constructor Details

    • FlywayTestExecutionListener

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

    • getLogger

      protected org.apache.commons.logging.Log getLogger()
      Returns:
      the instance of logger.
    • beforeTestClass

      public void beforeTestClass(org.springframework.test.context.TestContext testContext) throws Exception
      Invoke this method before test class will be created.

      Attention: This will be only invoked if spring version >= 3.x are used.
      Specified by:
      beforeTestClass in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      beforeTestClass in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • beforeTestExecution

      public void beforeTestExecution(org.springframework.test.context.TestContext testContext) throws Exception
      implementation for annotation FlywayTest for handling with org.testng.annotations.BeforeMethod annotation.
      Specified by:
      beforeTestExecution in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      beforeTestExecution in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • prepareTestInstance

      public void prepareTestInstance(org.springframework.test.context.TestContext testContext) throws Exception
      implementation for annotation FlywayTest for handling with org.junit.Before or BeforeEach annotation.
      Specified by:
      prepareTestInstance in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      prepareTestInstance in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • beforeTestMethod

      public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception
      Called from spring before a test method will be invoked.
      Specified by:
      beforeTestMethod in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      beforeTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • afterTestMethod

      public void afterTestMethod(org.springframework.test.context.TestContext testContext) throws Exception
      no implementation for annotation FlywayTest needed.
      Specified by:
      afterTestMethod in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      afterTestMethod in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • afterTestClass

      public void afterTestClass(org.springframework.test.context.TestContext testContext) throws Exception
      no implementation for annotation FlywayTest needed.
      Specified by:
      afterTestClass in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      afterTestClass in class org.springframework.test.context.support.AbstractTestExecutionListener
      Parameters:
      testContext - default test context filled from spring
      Throws:
      Exception - if any error occurred
    • 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