This extensions allows you to clean / migrate your database using Flyway during testing.
Add the following dependency to your project:
<dependency>
<groupId>com.radcortez.flyway</groupId>
<artifactId>flyway-junit5-extension</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
NOTE:
This project depends on:
Add the @FlywayTest
annotation to your test class or method. By default, Flyway will perform the migrate
action
before each test execution and the clean
action after the test. This can be disabled by turning clean = false
in
the @FlywayTest
annotation.
The only required information in the @FlywayTest
annotation is the database information that you can supply using
the inner @DataSource
annotation. In the @DataSource
annotation you can specify the url
, username
and password
to connect to a running database:
@FlywayTest(@DataSource(url = "jdbc:h2:mem:test"))
class JUnit5Test {
}
Or you can implement a DataSourceProvider
and return a DataSourceInfo
with the database connection details:
@FlywayTest(@DataSource(JUnit5Test.H2DatasourceProvider.class))
class JUnit5Test {
static class H2DatasourceProvider implements DataSourceProvider {
@Override
public DataSourceInfo getDatasourceInfo(final ExtensionContext extensionContext) {
return DataSourceInfo.config("jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1");
}
}
}
The DataSourceProvider
will always take priority over url
, username
and password
in the @DataSource
annotation.
The @FlywayTest
annotation can also be placed in a method.
@FlywayTest(additionalLocations = "db/additionalLocation")
void additionalLocations() throws Exception {
}
When both the class and the method are annotated, the annotations metadata is merged with the method annotation taking priority over the class annotation.
The extension uses the default path to load migration scripts from Flyway, set in resources/db/migration
.
If you want to add specific database migrations to a particular test, you can place the migration files in
resources/db/
plus the fully qualified name of the test as a path. For instance
com/radcortez/flyway/test/junit/H2LocationTest
.
Additional migration locations can be defined using the additionalLocations
metadata in the @FlywayTest
annotation.
This will not override the default locations, but just add a location for the migration files.
You can also place the @FlywayTest
annotation in a meta annotation and then use it in the test class.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@FlywayTest(value = @DataSource(url = "jdbc:h2:mem:test"))
public @interface H2 {
}
@H2
class H2MetaAnnotationTest {
}
The @H2
annotation is already available in the extension, but you need to remember to add the H2 dependency to your
project to be able to use an H2 database:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>test</scope>
</dependency>