Skip to content

Commit 7ab1dda

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 7ab1dda

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 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,12 @@ void getBindConstructorWhenHasTwoConstructorsWithBothConstructorBindingThrowsExc
9293
.withMessageContaining("has more than one @ConstructorBinding");
9394
}
9495

96+
@Test
97+
void getBindConstructorWhenIsTypeWithPrivateConstructorReturnsNull() {
98+
Constructor<?> constructor = this.provider.getBindConstructor(TypeWithPrivateConstructor.class, false);
99+
assertThat(constructor).isNull();
100+
}
101+
95102
@Test
96103
void getBindConstructorWhenIsMemberTypeWithPrivateConstructorReturnsNull() {
97104
Constructor<?> constructor = this.provider.getBindConstructor(MemberTypeWithPrivateConstructor.Member.class,
@@ -224,6 +231,13 @@ static class TwoConstructorsWithBothConstructorBinding {
224231

225232
}
226233

234+
static final class TypeWithPrivateConstructor {
235+
236+
private TypeWithPrivateConstructor(Environment environment) {
237+
}
238+
239+
}
240+
227241
static class MemberTypeWithPrivateConstructor {
228242

229243
static final class Member {

0 commit comments

Comments
 (0)