Add 'required' parameter to ConnectionDetailsFactories

Update `ConnectionDetailsFactories` so that callers can now declare if
a result is required or not and improve exception hierarchy.

See gh-35168
This commit is contained in:
Phillip Webb
2023-04-29 13:23:48 -07:00
parent 2b261e6ebd
commit 403481ff96
7 changed files with 89 additions and 36 deletions

View File

@@ -36,7 +36,6 @@ import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.test.context.MergedContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
@@ -85,7 +84,7 @@ class ServiceConnectionContextCustomizerTests {
given(context.getBeanFactory()).willReturn(beanFactory);
MergedContextConfiguration mergedConfig = mock(MergedContextConfiguration.class);
JdbcConnectionDetails connectionDetails = new TestJdbcConnectionDetails();
given(this.factories.getConnectionDetails(this.source))
given(this.factories.getConnectionDetails(this.source, true))
.willReturn(Map.of(JdbcConnectionDetails.class, connectionDetails));
customizer.customizeContext(context, mergedConfig);
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
@@ -96,18 +95,6 @@ class ServiceConnectionContextCustomizerTests {
assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class);
}
@Test
void customizeContextWhenFactoriesHasNoConnectionDetailsThrowsException() {
ServiceConnectionContextCustomizer customizer = new ServiceConnectionContextCustomizer(List.of(this.source),
this.factories);
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
given(context.getBeanFactory()).willReturn(beanFactory);
MergedContextConfiguration mergedConfig = mock(MergedContextConfiguration.class);
assertThatIllegalStateException().isThrownBy(() -> customizer.customizeContext(context, mergedConfig))
.withMessageStartingWith("No connection details created for @ServiceConnection source");
}
/**
* Test {@link JdbcConnectionDetails}.
*/