Skip to content

Commit

Permalink
Improve exception message to include affected configuration class
Browse files Browse the repository at this point in the history
Closes gh-32998
  • Loading branch information
snicoll committed Jun 11, 2024
1 parent a6fb7e9 commit 89e8942
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ protected final SourceClass doProcessConfigurationClass(
List<Condition> registerBeanConditions = collectRegisterBeanConditions(configClass);
if (!registerBeanConditions.isEmpty()) {
throw new ApplicationContextException(
"Component scan could not be used with conditions in REGISTER_BEAN phase: " + registerBeanConditions);
"Component scan for configuration class [%s] could not be used with conditions in REGISTER_BEAN phase: %s"
.formatted(configClass.getMetadata().getClassName(), registerBeanConditions));
}
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.Gh23206Tests.ConditionalConfiguration.NestedConfiguration;
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
import org.springframework.core.type.AnnotatedTypeMetadata;

Expand All @@ -39,7 +40,9 @@ void componentScanShouldFailWithRegisterBeanCondition() {
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh)
.withMessageContaining(ConditionalComponentScanConfiguration.class.getName())
.havingCause().isInstanceOf(ApplicationContextException.class)
.withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase");
.withMessageStartingWith("Component scan for configuration class [")
.withMessageContaining(ConditionalComponentScanConfiguration.class.getName())
.withMessageContaining("could not be used with conditions in REGISTER_BEAN phase");
}

@Test
Expand All @@ -49,7 +52,9 @@ void componentScanShouldFailWithRegisterBeanConditionOnClasThatImportedIt() {
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(context::refresh)
.withMessageContaining(ConditionalConfiguration.class.getName())
.havingCause().isInstanceOf(ApplicationContextException.class)
.withMessageContaining("Component scan could not be used with conditions in REGISTER_BEAN phase");
.withMessageStartingWith("Component scan for configuration class [")
.withMessageContaining(NestedConfiguration.class.getName())
.withMessageContaining("could not be used with conditions in REGISTER_BEAN phase");
}


Expand Down

0 comments on commit 89e8942

Please sign in to comment.