Skip to content

Commit ffa6d6d

Browse files
committed
Improve error message used in TestDatabaseAutoConfiguration
By default, `@DataJpaTest` (and `@AutoConfigureTestDatabase`) attempt to replace any existing `DataSource` by an embedded one. Previously, if there is was no embedded database on the classpath, the exception message did not provide that context in the error message. This commit clarifies the error message to conduct `TestDatabaseAutoConfiguration` (that is replacing the existing `DataSource`). Closes gh-7797
1 parent 80a1e1a commit ffa6d6d

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -193,9 +193,10 @@ public EmbeddedDatabase getEmbeddedDatabase() {
193193
connection = EmbeddedDatabaseConnection.get(getClass().getClassLoader());
194194
}
195195
Assert.state(connection != EmbeddedDatabaseConnection.NONE,
196-
"Cannot determine embedded database for tests. If you want "
197-
+ "an embedded database please put a supported one "
198-
+ "on the classpath.");
196+
"Failed to replace DataSource with an embedded database for tests. If "
197+
+ "you want an embedded database please put a supported one "
198+
+ "on the classpath or tune the replace attribute of "
199+
+ "@AutoconfigureTestDatabase.");
199200
return new EmbeddedDatabaseBuilder().generateUniqueName(true)
200201
.setType(connection.getType()).build();
201202
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.After;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import org.springframework.beans.factory.BeanCreationException;
26+
import org.springframework.boot.test.util.EnvironmentTestUtils;
27+
import org.springframework.boot.testutil.ClassPathExclusions;
28+
import org.springframework.boot.testutil.FilteredClassPathRunner;
29+
import org.springframework.context.ConfigurableApplicationContext;
30+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.mockito.Mockito.mock;
36+
37+
/**
38+
* Specific tests for {@link TestDatabaseAutoConfiguration} when no embedded database is
39+
* available.
40+
*
41+
* @author Stephane Nicoll
42+
*/
43+
@RunWith(FilteredClassPathRunner.class)
44+
@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" })
45+
public class TestDatabaseAutoConfigurationNoEmbeddedTests {
46+
47+
private ConfigurableApplicationContext context;
48+
49+
@After
50+
public void closeContext() {
51+
if (this.context != null) {
52+
this.context.close();
53+
}
54+
}
55+
56+
@Test
57+
public void applyAnyReplace() {
58+
try {
59+
load(ExistingDataSourceConfiguration.class);
60+
}
61+
catch (BeanCreationException ex) {
62+
String message = ex.getMessage();
63+
assertThat(message).contains(
64+
"Failed to replace DataSource with an embedded database for tests.");
65+
assertThat(message).contains(
66+
"If you want an embedded database please put a supported one on the "
67+
+ "classpath");
68+
assertThat(message).contains(
69+
"or tune the replace attribute of @AutoconfigureTestDatabase.");
70+
}
71+
}
72+
73+
@Test
74+
public void applyNoReplace() {
75+
load(ExistingDataSourceConfiguration.class, "spring.test.database.replace=NONE");
76+
assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(1);
77+
assertThat(this.context.getBean(DataSource.class)).isSameAs(
78+
this.context.getBean("myCustomDataSource"));
79+
}
80+
81+
public void load(Class<?> config, String... environment) {
82+
this.context = doLoad(config, environment);
83+
}
84+
85+
public ConfigurableApplicationContext doLoad(Class<?> config, String... environment) {
86+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
87+
if (config != null) {
88+
ctx.register(config);
89+
}
90+
ctx.register(TestDatabaseAutoConfiguration.class);
91+
EnvironmentTestUtils.addEnvironment(ctx, environment);
92+
ctx.refresh();
93+
return ctx;
94+
}
95+
96+
@Configuration
97+
static class ExistingDataSourceConfiguration {
98+
99+
@Bean
100+
public DataSource myCustomDataSource() {
101+
return mock(DataSource.class);
102+
}
103+
104+
}
105+
106+
}

0 commit comments

Comments
 (0)