Skip to content

Commit e1347d8

Browse files
committed
#29 improve support for Spring Boot's test annotations
1 parent d84b02b commit e1347d8

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ public class FlywayMigrationIntegrationTest {
120120

121121
Spring Boot provides several annotations to simplify writing integration tests.
122122
One of them is the `@DataJpaTest` annotation, which can be used when a test focuses only on JPA components.
123-
By default, tests annotated with `@DataJpaTest` will use an embedded in-memory database. **This in-memory database can be H2, Derby or HSQL, but not the PostgreSQL database**.
124-
To change that, you must first disable the default in-memory database by `@AutoConfigureTestDatabase(replace = NONE)` and enable PostgreSQL database by `@AutoConfigureEmbeddedDatabase` instead.
123+
By default, tests annotated with this annotation use an in-memory database.
124+
But if the `@DataJpaTest` annotation is used together with the `@AutoConfigureEmbeddedDatabase` annotation,
125+
the in-memory database is automatically disabled and replaced by an embedded postgres database.
125126

126127
```java
127128
@RunWith(SpringRunner.class)
128-
@AutoConfigureTestDatabase(replace = NONE)
129-
@AutoConfigureEmbeddedDatabase
130129
@DataJpaTest
130+
@AutoConfigureEmbeddedDatabase
131131
public class SpringDataJpaAnnotationTest {
132132
// class body...
133133
}
@@ -142,9 +142,8 @@ You can also consider creating a custom [composed annotation](https://github.com
142142
@Inherited
143143
@Target(ElementType.TYPE)
144144
@Retention(RetentionPolicy.RUNTIME)
145-
@AutoConfigureTestDatabase(replace = Replace.NONE)
146-
@AutoConfigureEmbeddedDatabase
147145
@DataJpaTest
146+
@AutoConfigureEmbeddedDatabase
148147
public @interface PostgresDataJpaTest {
149148

150149
@AliasFor(annotation = DataJpaTest.class)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.zonky.test.db.postgres;
1818

19+
import com.google.common.collect.ImmutableMap;
1920
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
2021
import io.zonky.test.db.AutoConfigureEmbeddedDatabase.EmbeddedDatabaseType;
2122
import io.zonky.test.db.AutoConfigureEmbeddedDatabase.Replace;
@@ -39,6 +40,8 @@
3940
import org.springframework.context.ConfigurableApplicationContext;
4041
import org.springframework.context.support.AbstractApplicationContext;
4142
import org.springframework.core.annotation.AnnotatedElementUtils;
43+
import org.springframework.core.env.ConfigurableEnvironment;
44+
import org.springframework.core.env.MapPropertySource;
4245
import org.springframework.test.context.ContextConfigurationAttributes;
4346
import org.springframework.test.context.ContextCustomizer;
4447
import org.springframework.test.context.ContextCustomizerFactory;
@@ -86,6 +89,8 @@ public PreloadableEmbeddedPostgresContextCustomizer(AutoConfigureEmbeddedDatabas
8689

8790
@Override
8891
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
92+
context.addBeanFactoryPostProcessor(new EnvironmentPostProcessor(context.getEnvironment()));
93+
8994
Class<?> testClass = mergedConfig.getTestClass();
9095
FlywayTest[] flywayAnnotations = findFlywayTestAnnotations(testClass);
9196

@@ -118,6 +123,27 @@ public int hashCode() {
118123
}
119124
}
120125

126+
protected static class EnvironmentPostProcessor implements BeanDefinitionRegistryPostProcessor {
127+
128+
private final ConfigurableEnvironment environment;
129+
130+
public EnvironmentPostProcessor(ConfigurableEnvironment environment) {
131+
this.environment = environment;
132+
}
133+
134+
@Override
135+
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
136+
environment.getPropertySources().addFirst(new MapPropertySource(
137+
PreloadableEmbeddedPostgresContextCustomizer.class.getSimpleName(),
138+
ImmutableMap.of("spring.test.database.replace", "NONE")));
139+
}
140+
141+
@Override
142+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
143+
// nothing to do
144+
}
145+
}
146+
121147
protected static class PreloadableEmbeddedPostgresRegistrar implements BeanDefinitionRegistryPostProcessor {
122148

123149
private final AutoConfigureEmbeddedDatabase databaseAnnotation;

0 commit comments

Comments
 (0)