|
|
|
|
@ -22,12 +22,10 @@ import java.util.List;
@@ -22,12 +22,10 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.core.annotation.AnnotationUtils; |
|
|
|
|
import org.springframework.lang.NonNull; |
|
|
|
|
import org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor.TestBeanOverrideMetadata; |
|
|
|
|
import org.springframework.test.context.bean.override.example.ExampleService; |
|
|
|
|
import org.springframework.test.context.bean.override.example.FailingExampleService; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
|
@ -45,7 +43,7 @@ class TestBeanOverrideProcessorTests {
@@ -45,7 +43,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void findTestBeanFactoryMethodFindsFromCandidateNames() { |
|
|
|
|
Class<?> clazz = MethodConventionConf.class; |
|
|
|
|
Class<?> clazz = MethodConventionTestCase.class; |
|
|
|
|
Class<?> returnType = ExampleService.class; |
|
|
|
|
|
|
|
|
|
Method method = findTestBeanFactoryMethod(clazz, returnType, "example1", "example2", "example3"); |
|
|
|
|
@ -55,7 +53,7 @@ class TestBeanOverrideProcessorTests {
@@ -55,7 +53,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void findTestBeanFactoryMethodNotFound() { |
|
|
|
|
Class<?> clazz = MethodConventionConf.class; |
|
|
|
|
Class<?> clazz = MethodConventionTestCase.class; |
|
|
|
|
Class<?> returnType = ExampleService.class; |
|
|
|
|
|
|
|
|
|
assertThatIllegalStateException() |
|
|
|
|
@ -68,7 +66,7 @@ class TestBeanOverrideProcessorTests {
@@ -68,7 +66,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void findTestBeanFactoryMethodTwoFound() { |
|
|
|
|
Class<?> clazz = MethodConventionConf.class; |
|
|
|
|
Class<?> clazz = MethodConventionTestCase.class; |
|
|
|
|
Class<?> returnType = ExampleService.class; |
|
|
|
|
|
|
|
|
|
assertThatIllegalStateException() |
|
|
|
|
@ -82,13 +80,13 @@ class TestBeanOverrideProcessorTests {
@@ -82,13 +80,13 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
@Test |
|
|
|
|
void findTestBeanFactoryMethodNoNameProvided() { |
|
|
|
|
assertThatIllegalArgumentException() |
|
|
|
|
.isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionConf.class, ExampleService.class)) |
|
|
|
|
.isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionTestCase.class, ExampleService.class)) |
|
|
|
|
.withMessage("At least one candidate method name is required"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void createMetaDataForUnknownExplicitMethod() throws Exception { |
|
|
|
|
Class<?> clazz = ExplicitMethodNameConf.class; |
|
|
|
|
Class<?> clazz = ExplicitMethodNameTestCase.class; |
|
|
|
|
Class<?> returnType = ExampleService.class; |
|
|
|
|
Field field = clazz.getField("a"); |
|
|
|
|
TestBean overrideAnnotation = field.getAnnotation(TestBean.class); |
|
|
|
|
@ -105,7 +103,7 @@ class TestBeanOverrideProcessorTests {
@@ -105,7 +103,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void createMetaDataForKnownExplicitMethod() throws Exception { |
|
|
|
|
Class<?> clazz = ExplicitMethodNameConf.class; |
|
|
|
|
Class<?> clazz = ExplicitMethodNameTestCase.class; |
|
|
|
|
Field field = clazz.getField("b"); |
|
|
|
|
TestBean overrideAnnotation = field.getAnnotation(TestBean.class); |
|
|
|
|
assertThat(overrideAnnotation).isNotNull(); |
|
|
|
|
@ -118,7 +116,7 @@ class TestBeanOverrideProcessorTests {
@@ -118,7 +116,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
@Test |
|
|
|
|
void createMetaDataForConventionBasedFactoryMethod() throws Exception { |
|
|
|
|
Class<?> returnType = ExampleService.class; |
|
|
|
|
Class<?> clazz = MethodConventionConf.class; |
|
|
|
|
Class<?> clazz = MethodConventionTestCase.class; |
|
|
|
|
Field field = clazz.getField("field"); |
|
|
|
|
TestBean overrideAnnotation = field.getAnnotation(TestBean.class); |
|
|
|
|
assertThat(overrideAnnotation).isNotNull(); |
|
|
|
|
@ -134,7 +132,7 @@ class TestBeanOverrideProcessorTests {
@@ -134,7 +132,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void failToCreateMetadataForOtherAnnotation() throws NoSuchFieldException { |
|
|
|
|
Class<?> clazz = MethodConventionConf.class; |
|
|
|
|
Class<?> clazz = MethodConventionTestCase.class; |
|
|
|
|
Field field = clazz.getField("field"); |
|
|
|
|
NonNull badAnnotation = AnnotationUtils.synthesizeAnnotation(NonNull.class); |
|
|
|
|
|
|
|
|
|
@ -145,26 +143,25 @@ class TestBeanOverrideProcessorTests {
@@ -145,26 +143,25 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class MethodConventionConf { |
|
|
|
|
static class MethodConventionTestCase { |
|
|
|
|
|
|
|
|
|
@TestBean(name = "someField") |
|
|
|
|
public ExampleService field; |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
ExampleService example1() { |
|
|
|
|
return new FailingExampleService(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static ExampleService example2() { |
|
|
|
|
return new FailingExampleService(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static ExampleService example4() { |
|
|
|
|
return new FailingExampleService(); |
|
|
|
|
static ExampleService example4() { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static class ExplicitMethodNameConf { |
|
|
|
|
static class ExplicitMethodNameTestCase { |
|
|
|
|
|
|
|
|
|
@TestBean(methodName = "explicit1") |
|
|
|
|
public ExampleService a; |
|
|
|
|
@ -173,7 +170,7 @@ class TestBeanOverrideProcessorTests {
@@ -173,7 +170,7 @@ class TestBeanOverrideProcessorTests {
|
|
|
|
|
public ExampleService b; |
|
|
|
|
|
|
|
|
|
static ExampleService explicit2() { |
|
|
|
|
return new FailingExampleService(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|