ConditionalOnSingleCandidateTests Class — spring-boot Architecture
Architecture documentation for the ConditionalOnSingleCandidateTests class in ConditionalOnSingleCandidateTests.java from the spring-boot codebase.
Entity Profile
Source Code
core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidateTests.java lines 37–337
class ConditionalOnSingleCandidateTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@Test
void singleCandidateNoCandidate() {
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
}
@Test
void singleCandidateOneCandidate() {
this.contextRunner.withUserConfiguration(AlphaConfiguration.class, OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).isEqualTo("alpha");
});
}
@Test
void singleCandidateOneScopedProxyCandidate() {
this.contextRunner
.withUserConfiguration(AlphaScopedProxyConfiguration.class, OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).hasToString("alpha");
});
}
@Test
void singleCandidateInAncestorsOneCandidateInCurrent() {
this.contextRunner.run((parent) -> this.contextRunner
.withUserConfiguration(AlphaConfiguration.class, OnBeanSingleCandidateInAncestorsConfiguration.class)
.withParent(parent)
.run((child) -> assertThat(child).doesNotHaveBean("consumer")));
}
@Test
void singleCandidateInAncestorsOneCandidateInParent() {
this.contextRunner.withUserConfiguration(AlphaConfiguration.class)
.run((parent) -> this.contextRunner
.withUserConfiguration(OnBeanSingleCandidateInAncestorsConfiguration.class)
.withParent(parent)
.run((child) -> {
assertThat(child).hasBean("consumer");
assertThat(child.getBean("consumer")).isEqualTo("alpha");
}));
}
@Test
void singleCandidateInAncestorsOneCandidateInGrandparent() {
this.contextRunner.withUserConfiguration(AlphaConfiguration.class)
.run((grandparent) -> this.contextRunner.withParent(grandparent)
.run((parent) -> this.contextRunner
.withUserConfiguration(OnBeanSingleCandidateInAncestorsConfiguration.class)
.withParent(parent)
.run((child) -> {
assertThat(child).hasBean("consumer");
assertThat(child.getBean("consumer")).isEqualTo("alpha");
})));
}
@Test
void singleCandidateMultipleCandidates() {
this.contextRunner
.withUserConfiguration(AlphaConfiguration.class, BravoConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
}
@Test
void singleCandidateMultipleCandidatesOnePrimary() {
this.contextRunner
.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).isEqualTo("alpha");
});
}
@Test
void singleCandidateTwoCandidatesOneNormalOneFallback() {
this.contextRunner
.withUserConfiguration(AlphaFallbackConfiguration.class, BravoConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).isEqualTo("bravo");
});
}
@Test
void singleCandidateMultipleCandidatesMultiplePrimary() {
this.contextRunner
.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoPrimaryConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
}
@Test
void singleCandidateMultipleCandidatesAllFallback() {
this.contextRunner
.withUserConfiguration(AlphaFallbackConfiguration.class, BravoFallbackConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
}
@Test
void invalidAnnotationTwoTypes() {
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateTwoTypesConfiguration.class).run((context) -> {
assertThat(context).hasFailed();
assertThat(context).getFailure()
.hasCauseInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(OnBeanSingleCandidateTwoTypesConfiguration.class.getName());
});
}
@Test
void invalidAnnotationNoType() {
this.contextRunner.withUserConfiguration(OnBeanSingleCandidateNoTypeConfiguration.class).run((context) -> {
assertThat(context).hasFailed();
assertThat(context).getFailure()
.hasCauseInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(OnBeanSingleCandidateNoTypeConfiguration.class.getName());
});
}
@Test
void singleCandidateMultipleCandidatesInContextHierarchy() {
this.contextRunner.withUserConfiguration(AlphaPrimaryConfiguration.class, BravoConfiguration.class)
.run((parent) -> this.contextRunner.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
.withParent(parent)
.run((child) -> {
assertThat(child).hasBean("consumer");
assertThat(child.getBean("consumer")).isEqualTo("alpha");
}));
}
@Test
void singleCandidateMultipleCandidatesOneAutowireCandidate() {
this.contextRunner
.withUserConfiguration(AlphaConfiguration.class, BravoNonAutowireConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).isEqualTo("alpha");
});
}
@Test
void singleCandidateMultipleCandidatesOneDefaultCandidate() {
this.contextRunner
.withUserConfiguration(AlphaConfiguration.class, BravoNonDefaultConfiguration.class,
OnBeanSingleCandidateConfiguration.class)
.run((context) -> {
assertThat(context).hasBean("consumer");
assertThat(context.getBean("consumer")).isEqualTo("alpha");
});
}
@Test
void singleCandidateDoesNotMatchWhenMultipleRegisteredAsSingletonCandidates() {
this.contextRunner.withInitializer((context) -> {
context.getBeanFactory().registerSingleton("alpha", "alpha");
context.getBeanFactory().registerSingleton("bravo", "bravo");
})
.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnSingleCandidate(String.class)
static class OnBeanSingleCandidateConfiguration {
@Bean
CharSequence consumer(CharSequence s) {
return s;
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.ANCESTORS)
static class OnBeanSingleCandidateInAncestorsConfiguration {
@Bean
CharSequence consumer(CharSequence s) {
return s;
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnSingleCandidate(value = String.class, type = "java.lang.Integer")
static class OnBeanSingleCandidateTwoTypesConfiguration {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnSingleCandidate
static class OnBeanSingleCandidateNoTypeConfiguration {
}
@Configuration(proxyBeanMethods = false)
static class AlphaConfiguration {
@Bean
String alpha() {
return "alpha";
}
}
@Configuration(proxyBeanMethods = false)
static class AlphaPrimaryConfiguration {
@Bean
@Primary
String alpha() {
return "alpha";
}
}
@Configuration(proxyBeanMethods = false)
static class AlphaFallbackConfiguration {
@Bean
@Fallback
String alpha() {
return "alpha";
}
}
@Configuration(proxyBeanMethods = false)
static class AlphaScopedProxyConfiguration {
@Bean
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
String alpha() {
return "alpha";
}
}
@Configuration(proxyBeanMethods = false)
static class BravoConfiguration {
@Bean
String bravo() {
return "bravo";
}
}
@Configuration(proxyBeanMethods = false)
static class BravoPrimaryConfiguration {
@Bean
@Primary
String bravo() {
return "bravo";
}
}
@Configuration(proxyBeanMethods = false)
static class BravoFallbackConfiguration {
@Bean
@Fallback
String bravo() {
return "bravo";
}
}
@Configuration(proxyBeanMethods = false)
static class BravoNonAutowireConfiguration {
@Bean(autowireCandidate = false)
String bravo() {
return "bravo";
}
}
@Configuration(proxyBeanMethods = false)
static class BravoNonDefaultConfiguration {
@Bean(defaultCandidate = false)
String bravo() {
return "bravo";
}
}
}
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free