Skip to content

Instantly share code, notes, and snippets.

@coderatchet
Created August 2, 2017 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coderatchet/cfa73134bd4bbb9b3acfe0dab7fb82bb to your computer and use it in GitHub Desktop.
Save coderatchet/cfa73134bd4bbb9b3acfe0dab7fb82bb to your computer and use it in GitHub Desktop.
Flyway Integration Test
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
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.
}
}
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