This commit reviews the structure of several classes to comply with
our guidelines. Also, rather than exposing a static method to configure
the context in a test, we call the high-level API directly.
This commit is contained in:
Stéphane Nicoll
2024-06-07 16:05:23 +02:00
parent 0165529d97
commit 02517e5011
7 changed files with 94 additions and 97 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.bean.override.example.ExampleBeanOverrideAnnotation;
import org.springframework.test.context.bean.override.example.ExampleService;
import org.springframework.test.context.bean.override.example.FailingExampleService;
@@ -47,6 +48,7 @@ import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link BeanOverrideBeanFactoryPostProcessor} combined with a
@@ -199,9 +201,9 @@ class BeanOverrideBeanFactoryPostProcessorTests {
});
}
private AnnotationConfigApplicationContext createContext(Class<?>... classes) {
private AnnotationConfigApplicationContext createContext(Class<?> testClass) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
BeanOverrideContextCustomizer.registerInfrastructure(context, Set.of(classes));
new BeanOverrideContextCustomizer(Set.of(testClass)).customizeContext(context, mock(MergedContextConfiguration.class));
return context;
}

View File

@@ -24,7 +24,7 @@ package org.springframework.test.context.bean.override.convention;
*/
interface TestBeanFactory {
public static String createTestMessage() {
static String createTestMessage() {
return "test";
}

View File

@@ -30,7 +30,6 @@ import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor.findTestBeanFactoryMethod;
/**
* Tests for {@link TestBeanOverrideProcessor}.
@@ -41,12 +40,15 @@ import static org.springframework.test.context.bean.override.convention.TestBean
*/
class TestBeanOverrideProcessorTests {
private final TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
@Test
void findTestBeanFactoryMethodFindsFromCandidateNames() {
Class<?> clazz = MethodConventionTestCase.class;
Class<?> returnType = ExampleService.class;
Method method = findTestBeanFactoryMethod(clazz, returnType, "example1", "example2", "example3");
Method method = this.processor.findTestBeanFactoryMethod(
clazz, returnType, "example1", "example2", "example3");
assertThat(method.getName()).isEqualTo("example2");
}
@@ -56,7 +58,7 @@ class TestBeanOverrideProcessorTests {
Class<?> clazz = SubTestCase.class;
Class<?> returnType = String.class;
Method method = findTestBeanFactoryMethod(clazz, returnType, "factory");
Method method = this.processor.findTestBeanFactoryMethod(clazz, returnType, "factory");
assertThat(method).isEqualTo(ReflectionUtils.findMethod(clazz, "factory"));
}
@@ -67,7 +69,7 @@ class TestBeanOverrideProcessorTests {
Class<?> returnType = ExampleService.class;
assertThatIllegalStateException()
.isThrownBy(() -> findTestBeanFactoryMethod(clazz, returnType, "example1", "example3"))
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, "example1", "example3"))
.withMessage("""
Failed to find a static test bean factory method in %s with return type %s \
whose name matches one of the supported candidates %s""",
@@ -80,7 +82,7 @@ class TestBeanOverrideProcessorTests {
Class<?> returnType = ExampleService.class;
assertThatIllegalStateException()
.isThrownBy(() -> findTestBeanFactoryMethod(clazz, returnType, "example2", "example4"))
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(clazz, returnType, "example2", "example4"))
.withMessage("""
Found %d competing static test bean factory methods in %s with return type %s \
whose name matches one of the supported candidates %s""".formatted(
@@ -90,7 +92,7 @@ class TestBeanOverrideProcessorTests {
@Test
void findTestBeanFactoryMethodNoNameProvided() {
assertThatIllegalArgumentException()
.isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionTestCase.class, ExampleService.class))
.isThrownBy(() -> this.processor.findTestBeanFactoryMethod(MethodConventionTestCase.class, ExampleService.class))
.withMessage("At least one candidate method name is required");
}
@@ -102,9 +104,8 @@ class TestBeanOverrideProcessorTests {
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
assertThat(overrideAnnotation).isNotNull();
TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
assertThatIllegalStateException()
.isThrownBy(() -> processor.createMetadata(overrideAnnotation, clazz, field))
.isThrownBy(() -> this.processor.createMetadata(overrideAnnotation, clazz, field))
.withMessage("""
Failed to find a static test bean factory method in %s with return type %s \
whose name matches one of the supported candidates %s""",
@@ -118,8 +119,7 @@ class TestBeanOverrideProcessorTests {
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
assertThat(overrideAnnotation).isNotNull();
TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
assertThat(processor.createMetadata(overrideAnnotation, clazz, field))
assertThat(this.processor.createMetadata(overrideAnnotation, clazz, field))
.isInstanceOf(TestBeanOverrideMetadata.class);
}
@@ -131,8 +131,7 @@ class TestBeanOverrideProcessorTests {
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
assertThat(overrideAnnotation).isNotNull();
TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
assertThatIllegalStateException().isThrownBy(() -> processor.createMetadata(
assertThatIllegalStateException().isThrownBy(() -> this.processor.createMetadata(
overrideAnnotation, clazz, field))
.withMessage("""
Failed to find a static test bean factory method in %s with return type %s \
@@ -146,8 +145,7 @@ class TestBeanOverrideProcessorTests {
Field field = clazz.getField("field");
NonNull badAnnotation = AnnotationUtils.synthesizeAnnotation(NonNull.class);
TestBeanOverrideProcessor processor = new TestBeanOverrideProcessor();
assertThatIllegalStateException().isThrownBy(() -> processor.createMetadata(badAnnotation, clazz, field))
assertThatIllegalStateException().isThrownBy(() -> this.processor.createMetadata(badAnnotation, clazz, field))
.withMessage("Invalid annotation passed to TestBeanOverrideProcessor: expected @TestBean" +
" on field %s.%s", field.getDeclaringClass().getName(), field.getName());
}