Skip to content

Commit 069c678

Browse files
committed
Polishing
1 parent aa4b226 commit 069c678

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanInheritanceIntegrationTests.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,36 @@
2020
import org.junit.jupiter.api.Nested;
2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.context.ApplicationContext;
25+
import org.springframework.test.context.bean.override.convention.AbstractTestBeanIntegrationTestCase.FakePojo;
26+
import org.springframework.test.context.bean.override.convention.AbstractTestBeanIntegrationTestCase.Pojo;
2427

2528
import static org.assertj.core.api.Assertions.assertThat;
2629

2730
/**
2831
* {@link TestBean @TestBean} inheritance integration tests for success scenarios.
2932
*
33+
* <p>Tests inheritance within a class hierarchy as well as "inheritance" within
34+
* an enclosing class hierarchy.
35+
*
3036
* @author Simon Baslé
3137
* @author Sam Brannen
3238
* @since 6.2
3339
* @see FailingTestBeanInheritanceIntegrationTests
3440
*/
3541
public class TestBeanInheritanceIntegrationTests {
3642

37-
static AbstractTestBeanIntegrationTestCase.Pojo enclosingClassBeanOverride() {
38-
return new AbstractTestBeanIntegrationTestCase.FakePojo("in enclosing test class");
43+
static Pojo enclosingClassBeanOverride() {
44+
return new FakePojo("in enclosing test class");
3945
}
4046

4147
@Nested
42-
@DisplayName("Concrete inherited test with correct @TestBean setup")
43-
public class ConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegrationTestCase {
48+
@DisplayName("Nested, concrete inherited tests with correct @TestBean setup")
49+
public class NestedConcreteTestBeanIntegrationTests extends AbstractTestBeanIntegrationTestCase {
50+
51+
@Autowired
52+
ApplicationContext ctx;
4453

4554
@TestBean(name = "pojo", methodName = "commonBeanOverride")
4655
Pojo pojo;
@@ -53,28 +62,28 @@ static Pojo someBeanTestOverride() {
5362
}
5463

5564
@Test
56-
void fieldInSupertypeMethodInType(ApplicationContext ctx) {
57-
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
58-
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
59-
}
60-
61-
@Test
62-
void fieldInTypeMethodInSuperType(ApplicationContext ctx) {
65+
void fieldInSubtypeWithFactoryMethodInSupertype() {
6366
assertThat(ctx.getBean("pojo")).as("applicationContext").hasToString("in superclass");
6467
assertThat(this.pojo.getValue()).as("injection point").isEqualTo("in superclass");
6568
}
6669

6770
@Test
68-
void fieldInTypeMethodInEnclosingClass(ApplicationContext ctx) {
69-
assertThat(ctx.getBean("pojo2")).as("applicationContext").hasToString("in enclosing test class");
70-
assertThat(this.pojo2.getValue()).as("injection point").isEqualTo("in enclosing test class");
71+
void fieldInSupertypeWithFactoryMethodInSubtype() {
72+
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
73+
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
7174
}
7275

7376
@Test
74-
void fieldInSupertypePrioritizeMethodInType(ApplicationContext ctx) {
77+
void fieldInSupertypeWithPrioritizedFactoryMethodInSubtype() {
7578
assertThat(ctx.getBean("someBean")).as("applicationContext").hasToString("someBeanOverride");
7679
assertThat(this.someBean.getValue()).as("injection point").isEqualTo("someBeanOverride");
7780
}
81+
82+
@Test
83+
void fieldInNestedClassWithFactoryMethodInEnclosingClass() {
84+
assertThat(ctx.getBean("pojo2")).as("applicationContext").hasToString("in enclosing test class");
85+
assertThat(this.pojo2.getValue()).as("injection point").isEqualTo("in enclosing test class");
86+
}
7887
}
7988

8089
}

spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessorTests.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222

2323
import org.junit.jupiter.api.Test;
2424

25-
import org.springframework.context.annotation.Bean;
2625
import org.springframework.core.annotation.AnnotationUtils;
2726
import org.springframework.lang.NonNull;
2827
import org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor.TestBeanOverrideMetadata;
2928
import org.springframework.test.context.bean.override.example.ExampleService;
30-
import org.springframework.test.context.bean.override.example.FailingExampleService;
3129

3230
import static org.assertj.core.api.Assertions.assertThat;
3331
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -45,7 +43,7 @@ class TestBeanOverrideProcessorTests {
4543

4644
@Test
4745
void findTestBeanFactoryMethodFindsFromCandidateNames() {
48-
Class<?> clazz = MethodConventionConf.class;
46+
Class<?> clazz = MethodConventionTestCase.class;
4947
Class<?> returnType = ExampleService.class;
5048

5149
Method method = findTestBeanFactoryMethod(clazz, returnType, "example1", "example2", "example3");
@@ -55,7 +53,7 @@ void findTestBeanFactoryMethodFindsFromCandidateNames() {
5553

5654
@Test
5755
void findTestBeanFactoryMethodNotFound() {
58-
Class<?> clazz = MethodConventionConf.class;
56+
Class<?> clazz = MethodConventionTestCase.class;
5957
Class<?> returnType = ExampleService.class;
6058

6159
assertThatIllegalStateException()
@@ -68,7 +66,7 @@ void findTestBeanFactoryMethodNotFound() {
6866

6967
@Test
7068
void findTestBeanFactoryMethodTwoFound() {
71-
Class<?> clazz = MethodConventionConf.class;
69+
Class<?> clazz = MethodConventionTestCase.class;
7270
Class<?> returnType = ExampleService.class;
7371

7472
assertThatIllegalStateException()
@@ -82,13 +80,13 @@ void findTestBeanFactoryMethodTwoFound() {
8280
@Test
8381
void findTestBeanFactoryMethodNoNameProvided() {
8482
assertThatIllegalArgumentException()
85-
.isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionConf.class, ExampleService.class))
83+
.isThrownBy(() -> findTestBeanFactoryMethod(MethodConventionTestCase.class, ExampleService.class))
8684
.withMessage("At least one candidate method name is required");
8785
}
8886

8987
@Test
9088
void createMetaDataForUnknownExplicitMethod() throws Exception {
91-
Class<?> clazz = ExplicitMethodNameConf.class;
89+
Class<?> clazz = ExplicitMethodNameTestCase.class;
9290
Class<?> returnType = ExampleService.class;
9391
Field field = clazz.getField("a");
9492
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
@@ -105,7 +103,7 @@ void createMetaDataForUnknownExplicitMethod() throws Exception {
105103

106104
@Test
107105
void createMetaDataForKnownExplicitMethod() throws Exception {
108-
Class<?> clazz = ExplicitMethodNameConf.class;
106+
Class<?> clazz = ExplicitMethodNameTestCase.class;
109107
Field field = clazz.getField("b");
110108
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
111109
assertThat(overrideAnnotation).isNotNull();
@@ -118,7 +116,7 @@ void createMetaDataForKnownExplicitMethod() throws Exception {
118116
@Test
119117
void createMetaDataForConventionBasedFactoryMethod() throws Exception {
120118
Class<?> returnType = ExampleService.class;
121-
Class<?> clazz = MethodConventionConf.class;
119+
Class<?> clazz = MethodConventionTestCase.class;
122120
Field field = clazz.getField("field");
123121
TestBean overrideAnnotation = field.getAnnotation(TestBean.class);
124122
assertThat(overrideAnnotation).isNotNull();
@@ -134,7 +132,7 @@ void createMetaDataForConventionBasedFactoryMethod() throws Exception {
134132

135133
@Test
136134
void failToCreateMetadataForOtherAnnotation() throws NoSuchFieldException {
137-
Class<?> clazz = MethodConventionConf.class;
135+
Class<?> clazz = MethodConventionTestCase.class;
138136
Field field = clazz.getField("field");
139137
NonNull badAnnotation = AnnotationUtils.synthesizeAnnotation(NonNull.class);
140138

@@ -145,26 +143,25 @@ void failToCreateMetadataForOtherAnnotation() throws NoSuchFieldException {
145143
}
146144

147145

148-
static class MethodConventionConf {
146+
static class MethodConventionTestCase {
149147

150148
@TestBean(name = "someField")
151149
public ExampleService field;
152150

153-
@Bean
154151
ExampleService example1() {
155-
return new FailingExampleService();
152+
return null;
156153
}
157154

158155
static ExampleService example2() {
159-
return new FailingExampleService();
156+
return null;
160157
}
161158

162-
public static ExampleService example4() {
163-
return new FailingExampleService();
159+
static ExampleService example4() {
160+
return null;
164161
}
165162
}
166163

167-
static class ExplicitMethodNameConf {
164+
static class ExplicitMethodNameTestCase {
168165

169166
@TestBean(methodName = "explicit1")
170167
public ExampleService a;
@@ -173,7 +170,7 @@ static class ExplicitMethodNameConf {
173170
public ExampleService b;
174171

175172
static ExampleService explicit2() {
176-
return new FailingExampleService();
173+
return null;
177174
}
178175
}
179176

0 commit comments

Comments
 (0)