Improve exception handling in bean override support

- Do not silently abort bean override processing if the ApplicationContext
  is not a BeanDefinitionRegistry.

- Include conflicting bean names in error messages instead of just the
  number of conflicting beans.

- Consistently throw IllegalStateException.

- etc.
This commit is contained in:
Sam Brannen
2024-06-03 16:57:19 +02:00
parent 7a11899c0c
commit acdd8a0015
7 changed files with 48 additions and 39 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.test.context.bean.override.convention;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
@@ -49,8 +51,8 @@ class FailingTestBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean definition to override: 0 bean definitions \
found of type %s (as required by annotated field '%s.example')"""
Unable to select a bean definition to override: found 0 bean definitions \
of type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
}
@@ -62,9 +64,9 @@ class FailingTestBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean definition to override: 2 bean definitions \
found of type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
Unable to select a bean definition to override: found 2 bean definitions \
of type %s (as required by annotated field '%s.example'): %s"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName(), List.of("bean1", "bean2"))))));
}

View File

@@ -16,6 +16,8 @@
package org.springframework.test.context.bean.override.mockito;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
@@ -49,8 +51,8 @@ class FailingMockitoBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean definition to override: 0 bean definitions \
found of type %s (as required by annotated field '%s.example')"""
Unable to select a bean definition to override: found 0 bean definitions \
of type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
}
@@ -62,9 +64,9 @@ class FailingMockitoBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean definition to override: 2 bean definitions \
found of type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
Unable to select a bean definition to override: found 2 bean definitions \
of type %s (as required by annotated field '%s.example'): %s"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName(), List.of("bean1", "bean2"))))));
}

View File

@@ -16,6 +16,8 @@
package org.springframework.test.context.bean.override.mockito;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
@@ -48,7 +50,7 @@ class FailingMockitoSpyBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean to override by wrapping: 0 bean instances found of \
Unable to select a bean to override by wrapping: found 0 bean instances of \
type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
}
@@ -61,9 +63,9 @@ class FailingMockitoSpyBeanByTypeIntegrationTests {
cause(
instanceOf(IllegalStateException.class),
message("""
Unable to select a bean to override by wrapping: 2 bean instances found of \
type %s (as required by annotated field '%s.example')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
Unable to select a bean to override by wrapping: found 2 bean instances of \
type %s (as required by annotated field '%s.example'): %s"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName(), List.of("bean1", "bean2"))))));
}

View File

@@ -44,7 +44,7 @@ class FailingMockitoSpyBeanIntegrationTests {
finishedWithFailure(
cause(instanceOf(IllegalStateException.class),
message("""
Unable to select a bean to override by wrapping: 0 bean instances found \
Unable to select a bean to override by wrapping: found 0 bean instances \
of type %s (as required by annotated field '%s.notPresent')"""
.formatted(ExampleService.class.getName(), testClass.getSimpleName())))));
}