CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 35
Usage flyway spring test
flyway-spring-test add a test annotation support FlywayTest you can use your Flyway installation scripts inside of unit tests to setup the database as you need.
The annotation can be used on class and method level.
The annotation can only be used if you are using spring-test. For a easier startup flyway-test-extension deliver a simple application context ready to use.
Inside the maven setup example you will configure the database connection and a basic directory for flyway SQL scripts. All provided application context can be found here.
Attention:
flyway-spring-test is currently based on Spring 4. If Spring 3 support is needed use flyway-spring3-test dependency instead.
A complete project that use flyway-spring-test you will find
- flyway-test-sample-spring3
- flyway-test-sample-spring4.
- flyway-test-sample-spring5.
- flyway-test-junit5.
- flyway-test-testng.
For your project under test we must extent your Maven pom.xml file.
First step we include the dependency to flyway-spring-test. The project will bring a set of dependencies so you additional only need dependency to junit and your database driver.
<dependency>
<groupId>org.flywaydb.flyway-test-extensions</groupId>
<artifactId>flyway-spring-test</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
Second show a example Maven profile to run test again database H2. If you are use a different database use the database specific jdbc driver and jdbc URL.
<profile>
<id>h2-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<flyway.driver>org.h2.Driver</flyway.driver>
<flyway.url>jdbc:h2:nio:${project.build.directory}/db/flywaytest.db</flyway.url>
<flyway.user>h2_test</flyway.user>
<flyway.password>h2_test</flyway.password>
<flyway.locations>sampletest3</flyway.locations>
</systemPropertyVariables>
<threadCount>1</threadCount>
</configuration>
<goals>
<goal>test</goal>
</goals>
<executions>
</executions>
</plugin>
</plugins>
</build>
</profile>
The used system properties will be used from the flywayContainerContext.xml file of flyway-spring-test
.
Used system properties:
- flyway.driver - name of the database jdbc driver
- flyway.url - connection url for the database (without username and password) here we use a H2 file database with NIO and the database file will be created
inside target/db
directory - flyway.user - login user name for the database
- flyway.password - login password for the database
- flyway.locations - basic directory for the SQL installation and migration SQL statements.
To execute any test with H2 database call
mvn test -P h2-test
For the test implementation it is important to setup spring test execution. A implementation example you will find here SpringSpringDifferentContextJUnitTest.java as extension of Spring4JUnitTest.java.
@RunWith(SpringRunner.class)
@ContextConfiguration(locationsForMirgation = {"/context/flywayContainerContext.xml" })
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
FlywayTestExecutionListener.class })
- For this setup we use the spring runner fpr JUnit.
- The ContextConfiguration specifies which spring xml files should be loaded.
If you need more than one configuration file specify as separate files in the location list.
The files normally will be searched in your classpath. - In the test execution listeners section we specify the flyway-spring-test extentsion !FlywayTestExecutionListener.
If you need a database setup before you start any test you can use the annotation a class level.
@FlywayTest
public class Spring3JUnitTest
The database will be reseted before any test should started.
If you need a database setup before you specific test method, you can use the annotation at method level.
@Test
@FlywayTest
public void dummyTestMethodLoad()
Now the database will be reseted before the test method will be executed.
A special version of this invocation is to extent the base directory with a list, so you can insert specific test SQL statements.
@Test
@FlywayTest(locations= { "loadmsql" })
public void loadMultibleSQLs()
With this setup the annotation will do two migration steps.
- clean database
- baseline database
- extend the default directory
sampletest3
with directoryloadmsql
and execute all migrations
If you need for each test a idetically database setup you can combine the @FlywayTest annotation together with the JUnit @Before annotation
@Before
@FlywayTest
public void beforeSetup()
Now the database will be reseted before each test method.
If you need a database setup for more than one database schemas or database instances and you have more than one Flyway configuration. In this case you can use the FlywayTests annotation to control the setup of your databases. This annotation can also be used at class, before or method level. Following exmple show the usage on class level.
@FlywayTests ( value = {
@FlywayTest(flywayName = "flyway1"), // Flyway configuration for database 1
@FlywayTest(flywayName = "flyway2") // Flyway configuration for database 2,
})
public class
The default sample application context you will find it here simple_applicationContext.xml This context are deprecated. Following application context should be used instead flywayContainerContext.xml
If you need some other features you can set up a own application context and used it inside the test class set up.
The importent here is that you must configure a database connection and the flyway setup.