You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Properly resolve @TestBean factory method within class hierarchy
Prior to this commit, the search algorithm used to locate a @TestBean
factory method within a test class hierarchy incorrectly found factory
methods declared in subclasses or nested test classes "below" the class
in which the @TestBean field was declared. This resulted in "duplicate
bean override" failures for @TestBean overrides which are clearly not
duplicates but rather "overrides of an override".
This commit ensures that @TestBean factory method resolution is
consistent in type hierarchies as well as in enclosing class
hierarchies (for @Nested test classes) by beginning the search for a
factory method in the class which declares the @TestBean field.
Closesgh-34204
Copy file name to clipboardExpand all lines: spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java
+3-3
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2002-2024 the original author or authors.
2
+
* Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ public TestBeanOverrideHandler createHandler(Annotation overrideAnnotation, Clas
67
67
MethodfactoryMethod;
68
68
if (!methodName.isBlank()) {
69
69
// If the user specified an explicit method name, search for that.
Copy file name to clipboardExpand all lines: spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanInheritanceIntegrationTests.java
+20-35
Original file line number
Diff line number
Diff line change
@@ -48,19 +48,16 @@ static Pojo puzzleBean() {
48
48
returnnewFakePojo("puzzle in enclosing class");
49
49
}
50
50
51
-
staticPojoenclosingClassBean() {
51
+
staticPojoenclosingClassFactoryMethod() {
52
52
returnnewFakePojo("in enclosing test class");
53
53
}
54
54
55
55
abstractstaticclassAbstractTestCase {
56
56
57
-
@TestBean
58
-
PojosomeBean;
59
-
60
57
@TestBean("otherBean")
61
58
PojootherBean;
62
59
63
-
@TestBean("thirdBean")
60
+
@TestBean
64
61
PojoanotherBean;
65
62
66
63
@TestBean
@@ -70,8 +67,8 @@ static Pojo otherBean() {
70
67
returnnewFakePojo("other in superclass");
71
68
}
72
69
73
-
staticPojothirdBean() {
74
-
returnnewFakePojo("third in superclass");
70
+
staticPojoanotherBean() {
71
+
returnnewFakePojo("another in superclass");
75
72
}
76
73
77
74
staticPojoenigmaBean() {
@@ -93,49 +90,42 @@ class NestedTests extends AbstractTestCase {
0 commit comments