Skip to content

Commit a31ef59

Browse files
authored
Merge pull request #63 from zonkyio/flyway-properties
#58 Fix problem with flyway datasource properties
2 parents a1ae71c + 6a96680 commit a31ef59

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/EmbeddedPostgresContextCustomizerFactory.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public int hashCode() {
143143

144144
protected static class EnvironmentPostProcessor implements BeanDefinitionRegistryPostProcessor {
145145

146+
private static final NullPlaceholder NULL = new NullPlaceholder();
147+
146148
private final ConfigurableEnvironment environment;
147149

148150
public EnvironmentPostProcessor(ConfigurableEnvironment environment) {
@@ -153,13 +155,32 @@ public EnvironmentPostProcessor(ConfigurableEnvironment environment) {
153155
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
154156
environment.getPropertySources().addFirst(new MapPropertySource(
155157
PreloadableEmbeddedPostgresContextCustomizer.class.getSimpleName(),
156-
ImmutableMap.of("spring.test.database.replace", "NONE")));
158+
ImmutableMap.of(
159+
"spring.test.database.replace", "NONE",
160+
"spring.flyway.url", NULL,
161+
"spring.flyway.user", NULL,
162+
"flyway.url", NULL,
163+
"flyway.user", NULL
164+
)));
157165
}
158166

159167
@Override
160168
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
161169
// nothing to do
162170
}
171+
172+
protected static class NullPlaceholder {
173+
174+
// this method is required to hook up org.springframework.core.convert.support.FallbackObjectToStringConverter
175+
public static NullPlaceholder valueOf(String value) {
176+
throw new IllegalStateException("This method should never be called!");
177+
}
178+
179+
@Override
180+
public String toString() {
181+
return null;
182+
}
183+
}
163184
}
164185

165186
protected static class PreloadableEmbeddedPostgresRegistrar implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.zonky.test.db;
2+
3+
import org.flywaydb.core.Flyway;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.test.context.TestPropertySource;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import javax.sql.DataSource;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@RunWith(SpringRunner.class)
17+
@AutoConfigureEmbeddedDatabase(beanName = "dataSource")
18+
@TestPropertySource(properties = {
19+
"flyway.url=jdbc:postgresql://localhost:5432/test",
20+
"flyway.user=flyway",
21+
"flyway.password=password",
22+
"flyway.schemas=test",
23+
})
24+
@JdbcTest
25+
public class SpringBootFlywayPropertiesIntegrationTest {
26+
27+
@Configuration
28+
static class Config {}
29+
30+
@Autowired
31+
private Flyway flyway;
32+
33+
@Autowired
34+
private DataSource dataSource;
35+
36+
@Test
37+
public void test() {
38+
assertThat(flyway.getDataSource()).isSameAs(dataSource);
39+
}
40+
}

embedded-database-spring-test/src/test/java/io/zonky/test/db/SpringBootIntegrationTest.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,32 @@
1616

1717
package io.zonky.test.db;
1818

19-
import org.flywaydb.core.Flyway;
2019
import org.flywaydb.test.annotation.FlywayTest;
2120
import org.junit.Test;
22-
import org.junit.experimental.categories.Category;
2321
import org.junit.runner.RunWith;
2422
import org.springframework.beans.factory.annotation.Autowired;
2523
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
26-
import org.springframework.context.annotation.Bean;
2724
import org.springframework.context.annotation.Configuration;
2825
import org.springframework.jdbc.core.JdbcTemplate;
26+
import org.springframework.test.context.TestPropertySource;
2927
import org.springframework.test.context.junit4.SpringRunner;
3028

31-
import javax.sql.DataSource;
3229
import java.util.List;
3330
import java.util.Map;
3431

35-
import io.zonky.test.category.FlywayIntegrationTests;
36-
import static io.zonky.test.util.FlywayTestUtils.createFlyway;
37-
3832
import static org.assertj.core.api.Assertions.assertThat;
3933
import static org.assertj.core.api.Assertions.entry;
4034

4135
@RunWith(SpringRunner.class)
42-
@Category(FlywayIntegrationTests.class)
4336
@AutoConfigureEmbeddedDatabase(beanName = "dataSource")
37+
@TestPropertySource(properties = "flyway.schemas=test")
4438
@JdbcTest
4539
public class SpringBootIntegrationTest {
4640

4741
private static final String SQL_SELECT_PERSONS = "select * from test.person";
4842

4943
@Configuration
50-
static class Config {
51-
52-
@Bean(initMethod = "migrate")
53-
public Flyway flyway(DataSource dataSource) throws Exception {
54-
return createFlyway(dataSource, "test");
55-
}
56-
}
44+
static class Config {}
5745

5846
@Autowired
5947
private JdbcTemplate jdbcTemplate;

0 commit comments

Comments
 (0)