From 4bf645c615ee0a244b92923f54ebbc73071b0b5b Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 24 Nov 2016 15:31:27 +0100 Subject: [PATCH] `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 --- .../jpa/TestDatabaseAutoConfiguration.java | 3 +- .../TestDatabaseAutoConfigurationTests.java | 42 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java index 3e018a5c06..2f2e13df14 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java @@ -196,7 +196,8 @@ public class TestDatabaseAutoConfiguration { "Cannot determine embedded database for tests. If you want " + "an embedded database please put a supported one " + "on the classpath."); - return new EmbeddedDatabaseBuilder().setType(connection.getType()).build(); + return new EmbeddedDatabaseBuilder().generateUniqueName(true) + .setType(connection.getType()).build(); } } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java index a27eef2f9f..afe99954ce 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java @@ -24,6 +24,11 @@ import org.junit.Test; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; 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; @@ -49,7 +54,30 @@ public class TestDatabaseAutoConfigurationTests { 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) { + this.context = doLoad(config, environment); + } + + public ConfigurableApplicationContext doLoad(Class config, String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); if (config != null) { ctx.register(config); @@ -57,7 +85,19 @@ public class TestDatabaseAutoConfigurationTests { ctx.register(TestDatabaseAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(ctx, environment); 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(); + } + } }