Skip to content

Commit 28ccf54

Browse files
committed
Ignore schema username when auto-configuring a test DB
Previously, when an embedded test database was being auto-configured any schema username and password specified by the user would result in a separate embedded test database being created to load the schema. This then left the actual test database without the schema causing test failures. This commit updates the test database auto-configuration to set the schema username to an empty string in a property source that's added first to the environment's property sources. This causes any schema username configured by the user to be ignored, preventing the creation of a separate database for schema.sql processing. Fixes gh-19321
1 parent c51c343 commit 28ccf54

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java

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

1717
package org.springframework.boot.test.autoconfigure.jdbc;
1818

19+
import java.util.Collections;
20+
1921
import javax.sql.DataSource;
2022

2123
import org.apache.commons.logging.Log;
@@ -40,7 +42,9 @@
4042
import org.springframework.context.annotation.Configuration;
4143
import org.springframework.core.Ordered;
4244
import org.springframework.core.annotation.Order;
45+
import org.springframework.core.env.ConfigurableEnvironment;
4346
import org.springframework.core.env.Environment;
47+
import org.springframework.core.env.MapPropertySource;
4448
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4549
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4650
import org.springframework.util.Assert;
@@ -168,6 +172,10 @@ private static class EmbeddedDataSourceFactory {
168172

169173
EmbeddedDataSourceFactory(Environment environment) {
170174
this.environment = environment;
175+
if (environment instanceof ConfigurableEnvironment) {
176+
((ConfigurableEnvironment) environment).getPropertySources().addFirst(new MapPropertySource(
177+
"testDatabase", Collections.singletonMap("spring.datasource.schema-username", "")));
178+
}
171179
}
172180

173181
EmbeddedDatabase getEmbeddedDatabase() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.orm.jpa;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Integration tests for {@link DataJpaTest @DataJpaTest} with schema credentials that
30+
* should be ignored to allow the auto-configured test database to be used.
31+
*
32+
* @author Andy Wilkinson
33+
*/
34+
@DataJpaTest
35+
@TestPropertySource(
36+
properties = { "spring.datasource.schema-username=alice", "spring.datasource.schema-password=secret" })
37+
class DataJpaTestSchemaCredentialsIntegrationTests {
38+
39+
@Autowired
40+
private DataSource dataSource;
41+
42+
@Test
43+
void replacesDefinedDataSourceWithEmbeddedDefault() throws Exception {
44+
String product = this.dataSource.getConnection().getMetaData().getDatabaseProductName();
45+
assertThat(product).isEqualTo("H2");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)