Polishing
This commit is contained in:
@@ -149,8 +149,10 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
||||
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
|
||||
}
|
||||
else if (enforceExistingDefinition) {
|
||||
throw new IllegalStateException("Unable to override bean '" + beanName + "': there is no " +
|
||||
"bean definition to replace with that name of type " + overrideMetadata.getBeanType());
|
||||
throw new IllegalStateException("""
|
||||
Unable to override bean: there is no bean definition to replace \
|
||||
with name [%s] and type [%s]."""
|
||||
.formatted(beanName, overrideMetadata.getBeanType()));
|
||||
}
|
||||
beanNameIncludingFactory = beanName;
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ public interface BeanOverrideProcessor {
|
||||
|
||||
/**
|
||||
* Create an {@link OverrideMetadata} instance for the given annotated field.
|
||||
* @param overrideAnnotation the composed annotation that defines the
|
||||
* {@link BeanOverride @BeanOverride} that triggers this processor
|
||||
* @param testClass the test class being processed
|
||||
* @param overrideAnnotation the composed annotation that declares the
|
||||
* {@link BeanOverride @BeanOverride} annotation that triggers this processor
|
||||
* @param testClass the test class to process
|
||||
* @param field the annotated field
|
||||
* @return the {@link OverrideMetadata} instance that should handle the
|
||||
* given field
|
||||
|
||||
@@ -81,4 +81,5 @@ final class TestBeanOverrideMetadata extends OverrideMetadata {
|
||||
public int hashCode() {
|
||||
return this.overrideMethod.hashCode() * 29 + super.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -676,7 +676,7 @@ class MockHttpServletRequestTests {
|
||||
void startAsyncShouldUpdateRequestState() {
|
||||
assertThat(request.isAsyncStarted()).isFalse();
|
||||
request.setAsyncSupported(true);
|
||||
AsyncContext asyncContext = request.startAsync();
|
||||
request.startAsync();
|
||||
assertThat(request.isAsyncStarted()).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@@ -69,8 +69,8 @@ class BeanOverrideBeanFactoryPostProcessorTests {
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("Unable to override bean 'descriptionBean': there is no bean definition " +
|
||||
"to replace with that name of type java.lang.String");
|
||||
.withMessage("Unable to override bean: there is no bean definition " +
|
||||
"to replace with name [descriptionBean] and type [java.lang.String].");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,8 +80,8 @@ class BeanOverrideBeanFactoryPostProcessorTests {
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("Unable to override bean 'descriptionBean': there is no bean definition " +
|
||||
"to replace with that name of type java.lang.String");
|
||||
.withMessage("Unable to override bean: there is no bean definition " +
|
||||
"to replace with name [descriptionBean] and type [java.lang.String].");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,7 +102,9 @@ class BeanOverrideBeanFactoryPostProcessorTests {
|
||||
|
||||
private AnnotationConfigApplicationContext prepareContextWithFactoryBean(Object objectTypeAttribute) {
|
||||
AnnotationConfigApplicationContext context = createContext(CaseOverrideBeanProducedByFactoryBean.class);
|
||||
// Register a TestFactoryBean that will not be overridden
|
||||
context.registerBean("testFactoryBean", TestFactoryBean.class, TestFactoryBean::new);
|
||||
// Register another TestFactoryBean that will be overridden
|
||||
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
||||
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, objectTypeAttribute);
|
||||
context.registerBeanDefinition("beanToBeOverridden", factoryBeanDefinition);
|
||||
@@ -142,7 +144,7 @@ class BeanOverrideBeanFactoryPostProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void replaceBeanByTypeWithMultipleMatchesAndFieldNameQualifierMatches() {
|
||||
void replaceBeanByTypeWithMultipleMatchesAndFieldNameAsFallbackQualifierMatches() {
|
||||
AnnotationConfigApplicationContext context = createContext(CaseByType.class);
|
||||
context.registerBean("counter", Integer.class, () -> 1);
|
||||
context.registerBean("someInteger", Integer.class, () -> 2);
|
||||
|
||||
@@ -44,12 +44,12 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
* @author Stephane Nicoll
|
||||
* @since 6.2
|
||||
*/
|
||||
public class OverrideMetadataTests {
|
||||
class OverrideMetadataTests {
|
||||
|
||||
@Test
|
||||
void forTestClassWithSingleField() {
|
||||
List<OverrideMetadata> overrideMetadata = OverrideMetadata.forTestClass(SingleAnnotation.class);
|
||||
assertThat(overrideMetadata).singleElement().satisfies(hasTestBeanMetadata(
|
||||
assertThat(overrideMetadata).singleElement().satisfies(hasOverrideMetadata(
|
||||
field(SingleAnnotation.class, "message"), String.class, null));
|
||||
}
|
||||
|
||||
@@ -57,25 +57,25 @@ public class OverrideMetadataTests {
|
||||
void forTestClassWithMultipleFields() {
|
||||
List<OverrideMetadata> overrideMetadata = OverrideMetadata.forTestClass(MultipleAnnotations.class);
|
||||
assertThat(overrideMetadata).hasSize(2)
|
||||
.anySatisfy(hasTestBeanMetadata(
|
||||
.anySatisfy(hasOverrideMetadata(
|
||||
field(MultipleAnnotations.class, "message"), String.class, null))
|
||||
.anySatisfy(hasTestBeanMetadata(
|
||||
.anySatisfy(hasOverrideMetadata(
|
||||
field(MultipleAnnotations.class, "counter"), Integer.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forTestClassWithMultipleFieldsSameMetadata() {
|
||||
void forTestClassWithMultipleFieldsWithIdenticalMetadata() {
|
||||
List<OverrideMetadata> overrideMetadata = OverrideMetadata.forTestClass(MultipleAnnotationsDuplicate.class);
|
||||
assertThat(overrideMetadata).hasSize(2)
|
||||
.anySatisfy(hasTestBeanMetadata(
|
||||
.anySatisfy(hasOverrideMetadata(
|
||||
field(MultipleAnnotationsDuplicate.class, "message1"), String.class, "messageBean"))
|
||||
.anySatisfy(hasTestBeanMetadata(
|
||||
.anySatisfy(hasOverrideMetadata(
|
||||
field(MultipleAnnotationsDuplicate.class, "message2"), String.class, "messageBean"));
|
||||
assertThat(new HashSet<>(overrideMetadata)).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void forTestClassWithDifferentOverrideMetadataOnSameField() {
|
||||
void forTestClassWithCompetingBeanOverrideAnnotationsOnSameField() {
|
||||
Field faultyField = field(MultipleAnnotationsOnSameField.class, "message");
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> OverrideMetadata.forTestClass(MultipleAnnotationsOnSameField.class))
|
||||
@@ -164,25 +164,27 @@ public class OverrideMetadataTests {
|
||||
assertThat(metadata).isNotEqualTo(metadata2);
|
||||
}
|
||||
|
||||
private OverrideMetadata createMetadata(Field field) {
|
||||
private static OverrideMetadata createMetadata(Field field) {
|
||||
return createMetadata(field, null);
|
||||
}
|
||||
|
||||
private OverrideMetadata createMetadata(Field field, @Nullable String name) {
|
||||
private static OverrideMetadata createMetadata(Field field, @Nullable String name) {
|
||||
return new DummyOverrideMetadata(field, field.getType(), name, BeanOverrideStrategy.REPLACE_DEFINITION);
|
||||
}
|
||||
|
||||
private Field field(Class<?> target, String fieldName) {
|
||||
private static Field field(Class<?> target, String fieldName) {
|
||||
Field field = ReflectionUtils.findField(target, fieldName);
|
||||
assertThat(field).isNotNull();
|
||||
return field;
|
||||
}
|
||||
|
||||
private Consumer<OverrideMetadata> hasTestBeanMetadata(Field field, Class<?> beanType, @Nullable String beanName) {
|
||||
private static Consumer<OverrideMetadata> hasOverrideMetadata(Field field, Class<?> beanType, @Nullable String beanName) {
|
||||
return hasOverrideMetadata(field, beanType, BeanOverrideStrategy.REPLACE_DEFINITION, beanName);
|
||||
}
|
||||
|
||||
private Consumer<OverrideMetadata> hasOverrideMetadata(Field field, Class<?> beanType, BeanOverrideStrategy strategy, @Nullable String beanName) {
|
||||
private static Consumer<OverrideMetadata> hasOverrideMetadata(Field field, Class<?> beanType, BeanOverrideStrategy strategy,
|
||||
@Nullable String beanName) {
|
||||
|
||||
return metadata -> {
|
||||
assertThat(metadata.getField()).isEqualTo(field);
|
||||
assertThat(metadata.getBeanType().toClass()).isEqualTo(beanType);
|
||||
@@ -196,7 +198,6 @@ public class OverrideMetadataTests {
|
||||
|
||||
@DummyBean
|
||||
String message;
|
||||
|
||||
}
|
||||
|
||||
static class MultipleAnnotations {
|
||||
@@ -215,7 +216,6 @@ public class OverrideMetadataTests {
|
||||
|
||||
@DummyBean(beanName = "messageBean")
|
||||
String message2;
|
||||
|
||||
}
|
||||
|
||||
static class MultipleAnnotationsOnSameField {
|
||||
@@ -229,37 +229,33 @@ public class OverrideMetadataTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfigA {
|
||||
static class ConfigA {
|
||||
|
||||
private ExampleService noQualifier;
|
||||
ExampleService noQualifier;
|
||||
|
||||
@Qualifier("test")
|
||||
private ExampleService directQualifier;
|
||||
ExampleService directQualifier;
|
||||
|
||||
@Qualifier("different")
|
||||
private ExampleService differentDirectQualifier;
|
||||
ExampleService differentDirectQualifier;
|
||||
|
||||
@CustomQualifier
|
||||
private ExampleService customQualifier;
|
||||
|
||||
ExampleService customQualifier;
|
||||
}
|
||||
|
||||
public static class ConfigB {
|
||||
static class ConfigB {
|
||||
|
||||
private ExampleService noQualifier;
|
||||
ExampleService noQualifier;
|
||||
|
||||
private ExampleService example;
|
||||
ExampleService example;
|
||||
|
||||
@Qualifier("test")
|
||||
private ExampleService directQualifier;
|
||||
|
||||
ExampleService directQualifier;
|
||||
}
|
||||
|
||||
// Simple OverrideMetadata implementation
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@DummyBean
|
||||
public @interface MetaDummyBean {}
|
||||
@interface MetaDummyBean {}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class TestBeanContextCustomizerEqualityTests {
|
||||
}
|
||||
|
||||
|
||||
private ContextCustomizer createContextCustomizer(Class<?> testClass) {
|
||||
private static ContextCustomizer createContextCustomizer(Class<?> testClass) {
|
||||
ContextCustomizer customizer = BeanOverrideContextCustomizerTestUtils.createContextCustomizer(testClass);
|
||||
assertThat(customizer).isNotNull();
|
||||
return customizer;
|
||||
@@ -89,7 +89,7 @@ class TestBeanContextCustomizerEqualityTests {
|
||||
@TestBean
|
||||
private String description;
|
||||
|
||||
private static String description() {
|
||||
static String description() {
|
||||
return "overridden";
|
||||
}
|
||||
}
|
||||
@@ -99,7 +99,7 @@ class TestBeanContextCustomizerEqualityTests {
|
||||
@TestBean(name = "descriptionBean")
|
||||
private String description;
|
||||
|
||||
private static String description() {
|
||||
static String description() {
|
||||
return "overridden";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ public class TestBeanTests {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to override bean 'beanToOverride': there is no bean definition \
|
||||
to replace with that name of type java.lang.String""");
|
||||
Unable to override bean: there is no bean definition \
|
||||
to replace with name [beanToOverride] and type [java.lang.String].""");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -78,9 +78,8 @@ public class TestBeanTests {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to override bean 'beanToOverride': there is no bean definition \
|
||||
to replace with that name of type %s""".formatted(
|
||||
String.class.getName()));
|
||||
Unable to override bean: there is no bean definition \
|
||||
to replace with name [beanToOverride] and type [java.lang.String].""");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,7 +129,7 @@ public class TestBeanTests {
|
||||
@TestBean
|
||||
private String example;
|
||||
|
||||
private static String example() {
|
||||
static String example() {
|
||||
throw new IllegalStateException("Should not be called");
|
||||
}
|
||||
}
|
||||
@@ -140,7 +139,7 @@ public class TestBeanTests {
|
||||
@TestBean(name = "beanToOverride")
|
||||
private String example;
|
||||
|
||||
private static String example() {
|
||||
static String example() {
|
||||
throw new IllegalStateException("Should not be called");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user