Do not silently return null when no constructor candidate is found

This commit updates ConstructorOrFactoryMethodResolver to throw an
exception if no constructor or factory method can be found for a given
bean definition.

This prevents code generation to happen on an incomplete view of the
bean to instantiate.

Closes gh-29052
This commit is contained in:
Stephane Nicoll
2022-09-06 15:22:57 +02:00
parent 8fbd2141b7
commit 903078a5b2
2 changed files with 15 additions and 16 deletions

View File

@@ -200,8 +200,9 @@ class ConstructorOrFactoryMethodResolverTests {
BeanDefinition beanDefinition = BeanDefinitionBuilder
.rootBeanDefinition(MultiConstructorSample.class)
.addConstructorArgValue(Locale.ENGLISH).getBeanDefinition();
Executable executable = resolve(new DefaultListableBeanFactory(), beanDefinition);
assertThat(executable).isNull();
assertThatIllegalStateException().isThrownBy(() -> resolve(new DefaultListableBeanFactory(), beanDefinition))
.withMessageContaining(MultiConstructorSample.class.getName())
.withMessageContaining("and argument types [java.util.Locale]");
}
@Test
@@ -212,8 +213,9 @@ class ConstructorOrFactoryMethodResolverTests {
.rootBeanDefinition(Locale.class, "getDefault")
.getBeanDefinition())
.getBeanDefinition();
Executable executable = resolve(new DefaultListableBeanFactory(), beanDefinition);
assertThat(executable).isNull();
assertThatIllegalStateException().isThrownBy(() -> resolve(new DefaultListableBeanFactory(), beanDefinition))
.withMessageContaining(MultiConstructorSample.class.getName())
.withMessageContaining("and argument types [java.util.Locale]");
}
@Test
@@ -338,7 +340,6 @@ class ConstructorOrFactoryMethodResolverTests {
}
@Nullable
private Executable resolve(DefaultListableBeanFactory beanFactory, BeanDefinition beanDefinition) {
return new ConstructorOrFactoryMethodResolver(beanFactory).resolve(beanDefinition);
}