-
Notifications
You must be signed in to change notification settings - Fork 35
How to use Flyway Test with Junit5 and Springframework 5
For usage FlywayTest annotation with Junit5 is a little different as with Junit 4.
A example setup exist in the project flyway-test-junit5
Here a step by step example:
-
Add flyway-test as dependency to your project.
Junit5 integration will only work with springframework 5.<dependency> <groupId>org.flywaydb.flyway-test-extensions</groupId> <artifactId>flyway-test</artifactId> <version>5.2.4</version> <scope>test</scope> </dependency>
-
Add Junit5 dependencies to your project
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-engine</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.0.2</version> <scope>test</scope> </dependency>
-
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>
-
Test class setup. Important use here the new Junit5 annotation @ExtendWith, @Test, ...
It exist three possibility to implement integrate it.-
Use annotation
@FlywayTestExtension
are shortcut combination withSprintExtension.class
import org.junit.jupiter.api.Test; import org.flywaydb.test.junit5.annotation.FlywayTestExtension; @ContextConfiguration(locations = { "/context/test_applicationContext.xml" }) @FlywayTestExtension @FlywayTest public class SampleTest { ... @BeforeEach ... @FlywayTest @Test public void currentTest() {
-
Use JUnit5 extension
FlywayTestExtension
together withSprintExtension.class
import org.junit.jupiter.api.Test; import org.flywaydb.test.junit5.FlywayTestExtension; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith({SpringExtension.class}) @ExtendWith({FlywayTestExtenstion.class}) @ContextConfiguration(locations = { "/context/test_applicationContext.xml" }) @FlywayTest public class SampleTest { ... @BeforeEach ... @FlywayTest @Test public void currentTest() {
-
With old Spring style configuration
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() {
-
Remarks
The annotation @FlywayTest in a JUnit5 environmnet work only together with SpringExtension.class.
@FlywayTest can be used as class, method annotation or together with Junit5 annotation @BeforeEach.
A combination with annotation @BeforeAll is not implemented. In this case use @FlywayTest as class annotation.