Skip to content

How to use Flyway Test with Junit5 and Springframework 5

Florian edited this page Nov 27, 2017 · 6 revisions

For usage FlywayTest annotation with Junit5 is a little different as with Junit 4.

A example setup exist in the project flyway-spring5-test

Here a step by step example:

  1. Add flyway-spring5-test as dependency to your project.
    Junit5 integration will only work with springframework 5 and flyway-spring-test is currently based on springframework 4.

    <dependency>
        <groupId>org.flywaydb.flyway-test-extensions</groupId>
        <artifactId>flyway-spring5-test</artifactId>
        <version>4.2.1.2</version>
        <scope>test</scope> 
    </dependency>
    
  2. Add Junit5 dependencies to your project

     <dependency>
     	<groupId>org.junit.jupiter</groupId>
     	<artifactId>junit-jupiter-api</artifactId>
     	<version>5.0.2</version>
     </dependency>
     <dependency>
     	<groupId>org.junit.jupiter</groupId>
     	<artifactId>junit-jupiter-engine</artifactId>
     	<version>5.0.2</version>
     </dependency>
     <dependency>
     	<groupId>org.junit.platform</groupId>
     	<artifactId>junit-platform-engine</artifactId>
     	<version>1.0.2</version>
     </dependency>
     <dependency>
     	<groupId>org.junit.platform</groupId>
     	<artifactId>junit-platform-launcher</artifactId>
     	<version>1.0.2</version>
     </dependency>
    
  3. Add Junit5 dependencies to surefire plugin. Currently we tested it only with surefire plugin 2.19.1

     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19.1</version>
     <dependencies>
         <dependency>
             <groupId>org.junit.platform</groupId>
             <artifactId>junit-platform-surefire-provider</artifactId>
             <version>1.0.1</version>
         </dependency>
         <dependency>
              <groupId>org.junit.jupiter</groupId>
              <artifactId>junit-jupiter-engine</artifactId>
              <version>5.0.2</version>
          </dependency>
       </dependencies>
    
  4. Test class setup. Important use here the new Junit5 annotation @ExtendWith, @Test, ...

      import org.junit.jupiter.api.Test;
      import org.junit.jupiter.api.extension.ExtendWith;
      import org.springframework.test.context.junit.jupiter.SpringExtension;
    
      @ExtendWith({SpringExtension.class}) 
      @ContextConfiguration(locations = { "/context/test_applicationContext.xml" })
      @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
           FlywayTestExecutionListener.class })
      @FlywayTest
      public class SampleTest {
          ...
          @BeforeEach ...
    
          @FlywayTest
          @Test
          public void currentTest() {