Skip to content

Commit 8c88950

Browse files
committed
Add tests to ensure private constructor is not used for value object binding
Sometimes we want to duplicate `@ConfigurationProperties` bean with different prefix, we should use the primary configuration properties as default of additional configuration properties instead of re-configuring them all, then we could inject that primary bean and copy its values as default via constructor, there are two ways to force constructor used for injection instead of binding, annotating it with `@Autowired` or marking it as `private`, and the former one is already covered by tests, this commit add test for latter one.
1 parent 8628f73 commit 8c88950

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/DefaultBindConstructorProviderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*
3434
* @author Phillip Webb
3535
* @author Madhura Bhave
36+
* @author Yanming Zhou
3637
*/
3738
class DefaultBindConstructorProviderTests {
3839

@@ -92,6 +93,13 @@ void getBindConstructorWhenHasTwoConstructorsWithBothConstructorBindingThrowsExc
9293
.withMessageContaining("has more than one @ConstructorBinding");
9394
}
9495

96+
@Test
97+
void getBindConstructorWhenIsTypeWithPrivateConstructorReturnsNull() {
98+
Constructor<?> constructor = this.provider.getBindConstructor(TypeWithPrivateConstructor.class,
99+
false);
100+
assertThat(constructor).isNull();
101+
}
102+
95103
@Test
96104
void getBindConstructorWhenIsMemberTypeWithPrivateConstructorReturnsNull() {
97105
Constructor<?> constructor = this.provider.getBindConstructor(MemberTypeWithPrivateConstructor.Member.class,
@@ -224,6 +232,13 @@ static class TwoConstructorsWithBothConstructorBinding {
224232

225233
}
226234

235+
static class TypeWithPrivateConstructor {
236+
237+
private TypeWithPrivateConstructor(Environment environment) {
238+
}
239+
240+
}
241+
227242
static class MemberTypeWithPrivateConstructor {
228243

229244
static final class Member {

0 commit comments

Comments
 (0)