Harmonize NoUniqueBeanDefinitionException message
This commit makes sure that the programmatic exception that is thrown by the cache abstraction uses the same message structure as a default message produced by NoUniqueBeanDefinitionException. Closes gh-33305
This commit is contained in:
@@ -270,13 +270,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
||||
setCacheManager(this.beanFactory.getBean(CacheManager.class));
|
||||
}
|
||||
catch (NoUniqueBeanDefinitionException ex) {
|
||||
StringBuilder message = new StringBuilder("no CacheResolver specified and expected a single CacheManager bean, but found ");
|
||||
message.append(ex.getNumberOfBeansFound());
|
||||
if (ex.getBeanNamesFound() != null) {
|
||||
message.append(": [").append(StringUtils.collectionToCommaDelimitedString(ex.getBeanNamesFound())).append("]");
|
||||
int numberOfBeansFound = ex.getNumberOfBeansFound();
|
||||
Collection<String> beanNamesFound = ex.getBeanNamesFound();
|
||||
|
||||
StringBuilder message = new StringBuilder("no CacheResolver specified and expected single matching CacheManager but found ");
|
||||
message.append(numberOfBeansFound);
|
||||
if (beanNamesFound != null) {
|
||||
message.append(": ").append(StringUtils.collectionToCommaDelimitedString(beanNamesFound));
|
||||
}
|
||||
String exceptionMessage = message.toString();
|
||||
|
||||
if (beanNamesFound != null) {
|
||||
throw new NoUniqueBeanDefinitionException(CacheManager.class, beanNamesFound, exceptionMessage);
|
||||
}
|
||||
else {
|
||||
throw new NoUniqueBeanDefinitionException(CacheManager.class, numberOfBeansFound, exceptionMessage);
|
||||
}
|
||||
message.append(" - mark one as primary or declare a specific CacheManager to use.");
|
||||
throw new NoUniqueBeanDefinitionException(CacheManager.class, ex.getNumberOfBeansFound(), message.toString());
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
throw new NoSuchBeanDefinitionException(CacheManager.class, "no CacheResolver specified - "
|
||||
|
||||
@@ -92,9 +92,12 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(MultiCacheManagerConfig.class);
|
||||
assertThatThrownBy(ctx::refresh)
|
||||
.isInstanceOf(NoUniqueBeanDefinitionException.class)
|
||||
.hasMessageContaining("no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]")
|
||||
.hasNoCause();
|
||||
.isInstanceOfSatisfying(NoUniqueBeanDefinitionException.class, ex -> {
|
||||
assertThat(ex.getMessage()).contains(
|
||||
"no CacheResolver specified and expected single matching CacheManager but found 2: cm1,cm2");
|
||||
assertThat(ex.getNumberOfBeansFound()).isEqualTo(2);
|
||||
assertThat(ex.getBeanNamesFound()).containsExactly("cm1", "cm2");
|
||||
}).hasNoCause();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user