`AutoconfigureTestDatabase` replace with a fresh database

Previously, two or more `@DataJpaTest` tests in the same test suite were
sharing the same in-memory database instance. This commit makes sure
that `TestDatabaseAutoConfiguration` creates a unique embedded database
for each test it operates on.

Closes gh-7473
pull/7529/head
Stephane Nicoll 8 years ago
parent 0359b2f6f5
commit 4bf645c615

@ -196,7 +196,8 @@ public class TestDatabaseAutoConfiguration {
"Cannot determine embedded database for tests. If you want " "Cannot determine embedded database for tests. If you want "
+ "an embedded database please put a supported one " + "an embedded database please put a supported one "
+ "on the classpath."); + "on the classpath.");
return new EmbeddedDatabaseBuilder().setType(connection.getType()).build(); return new EmbeddedDatabaseBuilder().generateUniqueName(true)
.setType(connection.getType()).build();
} }
} }

@ -24,6 +24,11 @@ import org.junit.Test;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -49,7 +54,30 @@ public class TestDatabaseAutoConfigurationTests {
assertThat(this.context.getBeansOfType(DataSource.class)).isEmpty(); assertThat(this.context.getBeansOfType(DataSource.class)).isEmpty();
} }
@Test
public void replaceWithUniqueDatabase() {
load(ExistingDataSourceConfiguration.class);
DataSource datasource = this.context.getBean(DataSource.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.execute("create table example (id int, name varchar);");
ConfigurableApplicationContext anotherContext = doLoad(
ExistingDataSourceConfiguration.class);
try {
DataSource anotherDatasource = anotherContext.getBean(DataSource.class);
JdbcTemplate anotherJdbcTemplate = new JdbcTemplate(anotherDatasource);
anotherJdbcTemplate.execute("create table example (id int, name varchar);");
}
finally {
anotherContext.close();
}
}
public void load(Class<?> config, String... environment) { public void load(Class<?> config, String... environment) {
this.context = doLoad(config, environment);
}
public ConfigurableApplicationContext doLoad(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
if (config != null) { if (config != null) {
ctx.register(config); ctx.register(config);
@ -57,7 +85,19 @@ public class TestDatabaseAutoConfigurationTests {
ctx.register(TestDatabaseAutoConfiguration.class); ctx.register(TestDatabaseAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(ctx, environment); EnvironmentTestUtils.addEnvironment(ctx, environment);
ctx.refresh(); ctx.refresh();
this.context = ctx; return ctx;
}
@Configuration
static class ExistingDataSourceConfiguration {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL);
return builder.build();
}
} }
} }

Loading…
Cancel
Save