Forked from coderatchet/FlywayIntegrationTests.java
Created
January 23, 2021 16:59
-
-
Save shawnritchie/8302a524f9eadf1fd3dbbe43cadbc8c2 to your computer and use it in GitHub Desktop.
Flyway Integration Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flyway.url=jdbc:oracle:thin:@localhost:1521:XE | |
flyway.user=MY_TEST_SCHEMA_USER | |
flyway.password=password | |
flyway.schemas=MY_TEST_SCHEMA_USER | |
flyway.table=SCHEMA_VERSION | |
flyway.baseline-version=0.0.0 | |
flyway.locations=classpath:db/migration/oracle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.tests; | |
import com.example.tests.config.TestDatabaseConfig; | |
import org.flywaydb.core.Flyway; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.test.context.ActiveProfiles; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.TestExecutionListeners; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | |
@RunWith(SpringRunner.class) | |
@ActiveProfiles({"oracle-testdb", "test"}) | |
@PropertySource("classpath:/config/application-oracle-testdb.properties") | |
@ContextConfiguration(classes = {TestDatabaseConfig.class, FlywayAutoConfiguration.class}) | |
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class}) | |
public class FlywayIntegrationTests { | |
@Autowired | |
Flyway flyway; | |
@Before | |
public void setUp() throws Exception { | |
flyway.clean(); | |
flyway.baseline(); | |
flyway.migrate(); | |
} | |
@Test | |
public void testFlywayTestIsWorking() throws Exception { | |
// do stuff with the database, it works now. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.tests.config; | |
import org.flywaydb.core.Flyway; | |
import org.flywaydb.core.api.MigrationVersion; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.autoconfigure.flyway.FlywayProperties; | |
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
import org.springframework.context.annotation.*; | |
/** | |
* Cre | |
* <p> | |
* Explicit test profile ensures no production databases are inexplicably affected. | |
*/ | |
@Configuration | |
@Profile("test") | |
@PropertySource(value = "classpath:/config/application-oracle-testdb.properties", name = "props") | |
@EnableConfigurationProperties(FlywayProperties.class) | |
public class TestDatabaseConfig { | |
@Value("${flyway.table}") | |
private String table; | |
@Value("${flyway.baseline-version}") | |
private String baselineVersion; | |
@Bean | |
@Primary | |
@Autowired | |
public Flyway flyway(FlywayProperties flywayProperties) { | |
Flyway flyway = new Flyway(); | |
flyway.setDataSource(flywayProperties.getUrl(), flywayProperties.getUser(), flywayProperties.getPassword()); | |
flyway.setLocations(flywayProperties.getLocations().toArray(new String[0])); | |
flyway.setBaselineVersion(MigrationVersion.fromVersion(baselineVersion)); | |
flyway.setTable(table); | |
flyway.setBaselineOnMigrate(true); | |
return flyway; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment