Skip to content

Commit 37879d8

Browse files
committed
Test that TestBean can override a ConfigurationProperties bean
Closes gh-33969
1 parent 0ad5aa7 commit 37879d8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2024 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.context.properties;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.test.context.TestPropertySource;
24+
import org.springframework.test.context.bean.override.convention.TestBean;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for overriding {@link ConfigurationProperties @ConfigurationProperties} with
31+
* {@link TestBean @TestBean}.
32+
*
33+
* @author Andy Wilkinson
34+
*/
35+
@SpringJUnitConfig
36+
@TestPropertySource(properties = "immutable.property=test-property-source")
37+
class ConfigurationPropertiesTestBeanTests {
38+
39+
@TestBean
40+
private ImmutableProperties properties;
41+
42+
@Autowired
43+
private SomeConfiguration someConfiguration;
44+
45+
@Test
46+
void propertiesCanBeOverriddenUsingTestBean() {
47+
assertThat(this.properties.property).isEqualTo("test-bean");
48+
assertThat(this.someConfiguration.properties.property).isEqualTo("test-bean");
49+
}
50+
51+
static ImmutableProperties properties() {
52+
return new ImmutableProperties("test-bean");
53+
}
54+
55+
@Configuration(proxyBeanMethods = false)
56+
@EnableConfigurationProperties(ImmutableProperties.class)
57+
static class SomeConfiguration {
58+
59+
private final ImmutableProperties properties;
60+
61+
SomeConfiguration(ImmutableProperties properties) {
62+
this.properties = properties;
63+
}
64+
65+
}
66+
67+
@ConfigurationProperties("immutable")
68+
static class ImmutableProperties {
69+
70+
private final String property;
71+
72+
ImmutableProperties(String property) {
73+
this.property = property;
74+
}
75+
76+
String getProperty() {
77+
return this.property;
78+
}
79+
80+
}
81+
82+
}

0 commit comments

Comments
 (0)