Polishing
This commit is contained in:
@@ -20,13 +20,19 @@ import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.bean.override.convention.AbstractTestBeanIntegrationTestCase.FakePojo;
|
||||
import org.springframework.test.context.bean.override.convention.AbstractTestBeanIntegrationTestCase.Pojo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* {@link TestBean @TestBean} inheritance integration tests for success scenarios.
|
||||
*
|
||||
* <p>Tests inheritance within a class hierarchy as well as "inheritance" within
|
||||
* an enclosing class hierarchy.
|
||||
*
|
||||
* @author Simon Baslé
|
||||
* @author Sam Brannen
|
||||
* @since 6.2
|
||||
@@ -34,13 +40,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class TestBeanInheritanceIntegrationTests {
|
||||
|
||||
static AbstractTestBeanIntegrationTestCase.Pojo enclosingClassBeanOverride() {
|
||||
return new AbstractTestBeanIntegrationTestCase.FakePojo("in enclosing test class");
|
||||
static Pojo enclosingClassBeanOverride() {
|
||||
return new FakePojo("in enclosing test class");
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Concrete inherited test with correct @TestBean setup")
|
||||
public class ConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegrationTestCase {
|
||||
@DisplayName("Nested, concrete inherited tests with correct @TestBean setup")
|
||||
public class NestedConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegrationTestCase {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext ctx;
|
||||
|
||||
@TestBean(name = "pojo", methodName = "commonBeanOverride")
|
||||
Pojo pojo;
|
||||
@@ -53,28 +62,28 @@ public class TestBeanInheritanceIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldInSupertypeMethodInType(ApplicationContext ctx) {
|
||||
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
|
||||
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldInTypeMethodInSuperType(ApplicationContext ctx) {
|
||||
void fieldInSubtypeWithFactoryMethodInSupertype() {
|
||||
assertThat(ctx.getBean("pojo")).as("applicationContext").hasToString("in superclass");
|
||||
assertThat(this.pojo.getValue()).as("injection point").isEqualTo("in superclass");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldInTypeMethodInEnclosingClass(ApplicationContext ctx) {
|
||||
assertThat(ctx.getBean("pojo2")).as("applicationContext").hasToString("in enclosing test class");
|
||||
assertThat(this.pojo2.getValue()).as("injection point").isEqualTo("in enclosing test class");
|
||||
void fieldInSupertypeWithFactoryMethodInSubtype() {
|
||||
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
|
||||
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldInSupertypePrioritizeMethodInType(ApplicationContext ctx) {
|
||||
void fieldInSupertypeWithPrioritizedFactoryMethodInSubtype() {
|
||||
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
|
||||
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fieldInNestedClassWithFactoryMethodInEnclosingClass() {
|
||||
assertThat(ctx.getBean("pojo2")).as("applicationContext").hasToString("in enclosing test class");
|
||||
assertThat(this.pojo2.getValue()).as("injection point").isEqualTo("in enclosing test class");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@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 {
|
||||
|
||||
@Test
|
||||
void findTestBeanFactoryMethodNotFound() {
|
||||
Class<?> clazz = MethodConventionConf.class;
|
||||
Class<?> clazz = MethodConventionTestCase.class;
|
||||
Class<?> returnType = ExampleService.class;
|
||||
|
||||
assertThatIllegalStateException()
|
||||
@@ -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 {
|
||||
@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 {
|
||||
|
||||
@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 {
|
||||
@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 {
|
||||
|
||||
@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 {
|
||||
}
|
||||
|
||||
|
||||
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 {
|
||||
public ExampleService b;
|
||||
|
||||
static ExampleService explicit2() {
|
||||
return new FailingExampleService();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user