Stop using a conventional suffix for TestBean factory methods
This commit changes how factory method for `@TestBean` usage is discovered. Previously the field name or bean name suffixed with 'TestOverride' was used. It sounds more natural to just use the field name or bean name, leaving cases where a suffix is required to explicitly providing the method name. As part of this change, the exception messages have been revisited as it's less since the method name candidates have the exact same name as the field or bean name. A `()` is added to make it more clear the name is for a method. Closes gh-32940
This commit is contained in:
@@ -32,11 +32,11 @@ abstract class AbstractTestBeanIntegrationTestCase {
|
||||
@TestBean(name = "thirdBean")
|
||||
Pojo anotherBean;
|
||||
|
||||
static Pojo otherBeanTestOverride() {
|
||||
static Pojo otherBean() {
|
||||
return new FakePojo("otherBean in superclass");
|
||||
}
|
||||
|
||||
static Pojo thirdBeanTestOverride() {
|
||||
static Pojo thirdBean() {
|
||||
return new FakePojo("third in superclass");
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class FailingTestBeanByTypeIntegrationTests {
|
||||
void test() {
|
||||
}
|
||||
|
||||
static ExampleService exampleTestOverride() {
|
||||
static ExampleService example() {
|
||||
return fail("unexpected override");
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ class FailingTestBeanByTypeIntegrationTests {
|
||||
void test() {
|
||||
}
|
||||
|
||||
static ExampleService exampleTestOverride() {
|
||||
static ExampleService example() {
|
||||
return fail("unexpected override");
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.test.context.bean.override.convention;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.test.context.bean.override.convention.AbstractTestBeanIntegrationTestCase.Pojo;
|
||||
import org.springframework.test.context.junit.EngineTestKitUtils;
|
||||
|
||||
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
|
||||
@@ -40,10 +41,8 @@ class FailingTestBeanInheritanceIntegrationTests {
|
||||
EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1,
|
||||
finishedWithFailure(
|
||||
instanceOf(IllegalStateException.class),
|
||||
message("""
|
||||
Failed to find a static test bean factory method in %s with return type %s \
|
||||
whose name matches one of the supported candidates [someBeanTestOverride]"""
|
||||
.formatted(testClass.getName(), AbstractTestBeanIntegrationTestCase.Pojo.class.getName()))));
|
||||
message("No static method found named someBean() in %s with return type %s"
|
||||
.formatted(FieldInSupertypeButNoMethodTestCase.class.getName(), Pojo.class.getName()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,11 +51,8 @@ class FailingTestBeanInheritanceIntegrationTests {
|
||||
EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1,
|
||||
finishedWithFailure(
|
||||
instanceOf(IllegalStateException.class),
|
||||
message("""
|
||||
Found 2 competing static test bean factory methods in %s with return type %s \
|
||||
whose name matches one of the supported candidates \
|
||||
[anotherBeanTestOverride, thirdBeanTestOverride]"""
|
||||
.formatted(testClass.getName(), AbstractTestBeanIntegrationTestCase.Pojo.class.getName()))));
|
||||
message("Found 2 competing static methods named anotherBean() or thirdBean() in %s with return type %s"
|
||||
.formatted(Method1InSupertypeAndMethod2InTypeTestCase.class.getName(), Pojo.class.getName()))));
|
||||
}
|
||||
|
||||
|
||||
@@ -69,11 +65,11 @@ class FailingTestBeanInheritanceIntegrationTests {
|
||||
|
||||
static class Method1InSupertypeAndMethod2InTypeTestCase extends AbstractTestBeanIntegrationTestCase {
|
||||
|
||||
static Pojo someBeanTestOverride() {
|
||||
static Pojo someBean() {
|
||||
return new FakePojo("ignored");
|
||||
}
|
||||
|
||||
static Pojo anotherBeanTestOverride() {
|
||||
static Pojo anotherBean() {
|
||||
return new FakePojo("sub2");
|
||||
}
|
||||
|
||||
|
||||
@@ -66,25 +66,21 @@ class FailingTestBeanIntegrationTests {
|
||||
|
||||
@Test
|
||||
void testBeanFailingNoImplicitMethod() {
|
||||
Class<?> testClass = ExplicitTestOverrideMethodNotPresentTestCase.class;
|
||||
Class<?> testClass = ExplicitOverrideMethodNotPresentTestCase.class;
|
||||
EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1,
|
||||
finishedWithFailure(
|
||||
instanceOf(IllegalStateException.class),
|
||||
message("""
|
||||
Failed to find a static test bean factory method in %s with return type \
|
||||
java.lang.String whose name matches one of the supported candidates \
|
||||
[notPresent]""".formatted(testClass.getName()))));
|
||||
message("No static method found named notPresent() in %s with return type %s"
|
||||
.formatted(testClass.getName(), String.class.getName()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBeanFailingNoExplicitMethod() {
|
||||
Class<?> testClass = ImplicitTestOverrideMethodNotPresentTestCase.class;
|
||||
Class<?> testClass = ImplicitOverrideMethodNotPresentTestCase.class;
|
||||
EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1,
|
||||
finishedWithFailure(instanceOf(IllegalStateException.class),
|
||||
message("""
|
||||
Failed to find a static test bean factory method in %s with return type \
|
||||
java.lang.String whose name matches one of the supported candidates \
|
||||
[fieldTestOverride]""".formatted(testClass.getName()))));
|
||||
message("No static method found named field() in %s with return type %s"
|
||||
.formatted(testClass.getName(), String.class.getName()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +107,7 @@ class FailingTestBeanIntegrationTests {
|
||||
fail("should fail earlier");
|
||||
}
|
||||
|
||||
static String noOriginalBeanTestOverride() {
|
||||
static String noOriginalBean() {
|
||||
return "should be ignored";
|
||||
}
|
||||
}
|
||||
@@ -127,13 +123,13 @@ class FailingTestBeanIntegrationTests {
|
||||
fail("should fail earlier");
|
||||
}
|
||||
|
||||
static String notPresentTestOverride() {
|
||||
static String notPresent() {
|
||||
return "should be ignored";
|
||||
}
|
||||
}
|
||||
|
||||
@SpringJUnitConfig
|
||||
static class ExplicitTestOverrideMethodNotPresentTestCase {
|
||||
static class ExplicitOverrideMethodNotPresentTestCase {
|
||||
|
||||
@TestBean(methodName = "notPresent")
|
||||
String field;
|
||||
@@ -145,9 +141,9 @@ class FailingTestBeanIntegrationTests {
|
||||
}
|
||||
|
||||
@SpringJUnitConfig
|
||||
static class ImplicitTestOverrideMethodNotPresentTestCase {
|
||||
static class ImplicitOverrideMethodNotPresentTestCase {
|
||||
|
||||
@TestBean // expects fieldTestOverride method
|
||||
@TestBean // expects field method
|
||||
String field;
|
||||
|
||||
@Test
|
||||
@@ -167,7 +163,7 @@ class FailingTestBeanIntegrationTests {
|
||||
fail("should fail earlier");
|
||||
}
|
||||
|
||||
static String fieldTestOverride() {
|
||||
static String field() {
|
||||
return "should be ignored";
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public class TestBeanByTypeIntegrationTests {
|
||||
@CustomQualifier
|
||||
StringBuilder anyNameForStringBuilder2;
|
||||
|
||||
static ExampleService anyNameForServiceTestOverride() {
|
||||
static ExampleService anyNameForService() {
|
||||
return new RealExampleService("Mocked greeting");
|
||||
}
|
||||
|
||||
|
||||
@@ -57,12 +57,12 @@ public class TestBeanInheritanceIntegrationTests {
|
||||
@TestBean(name = "pojo2", methodName = "enclosingClassBeanOverride")
|
||||
Pojo pojo2;
|
||||
|
||||
static Pojo someBeanTestOverride() {
|
||||
static Pojo someBean() {
|
||||
return new FakePojo("someBeanOverride");
|
||||
}
|
||||
|
||||
// Hides otherBeanTestOverride() defined in AbstractTestBeanIntegrationTestCase.
|
||||
static Pojo otherBeanTestOverride() {
|
||||
// Hides otherBean() defined in AbstractTestBeanIntegrationTestCase.
|
||||
static Pojo otherBean() {
|
||||
return new FakePojo("otherBean in subclass");
|
||||
}
|
||||
|
||||
|
||||
@@ -50,17 +50,17 @@ public class TestBeanIntegrationTests {
|
||||
@TestBean(name = "nestedField")
|
||||
String renamed2;
|
||||
|
||||
@TestBean(name = "methodRenamed1", methodName = "fieldTestOverride")
|
||||
@TestBean(name = "methodRenamed1", methodName = "field")
|
||||
String methodRenamed1;
|
||||
|
||||
@TestBean(name = "methodRenamed2", methodName = "nestedFieldTestOverride")
|
||||
@TestBean(name = "methodRenamed2", methodName = "nestedField")
|
||||
String methodRenamed2;
|
||||
|
||||
static String fieldTestOverride() {
|
||||
static String field() {
|
||||
return "fieldOverride";
|
||||
}
|
||||
|
||||
static String nestedFieldTestOverride() {
|
||||
static String nestedField() {
|
||||
return "nestedFieldOverride";
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ public class TestBeanIntegrationTests {
|
||||
@DisplayName("With factory method in enclosing class")
|
||||
public class TestBeanFactoryMethodInEnclosingClassTests {
|
||||
|
||||
@TestBean(methodName = "nestedFieldTestOverride", name = "nestedField")
|
||||
@TestBean(methodName = "nestedField", name = "nestedField")
|
||||
String nestedField2;
|
||||
|
||||
@Test
|
||||
@@ -146,7 +146,7 @@ public class TestBeanIntegrationTests {
|
||||
@DisplayName("With factory method in the enclosing class of the enclosing class")
|
||||
public class TestBeanFactoryMethodInEnclosingClassLevel2Tests {
|
||||
|
||||
@TestBean(methodName = "nestedFieldTestOverride", name = "nestedField")
|
||||
@TestBean(methodName = "nestedField", name = "nestedField")
|
||||
String nestedField2;
|
||||
|
||||
@Test
|
||||
|
||||
@@ -54,8 +54,8 @@ class TestBeanOverrideMetadataTests {
|
||||
void forTestClassWithMissingMethod() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() ->OverrideMetadata.forTestClass(SampleMissingMethod.class))
|
||||
.withMessageStartingWith("Failed to find a static test bean factory method")
|
||||
.withMessageContaining("messageTestOverride");
|
||||
.withMessage("No static method found named message() in %s with return type %s",
|
||||
SampleMissingMethod.class.getName(), String.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -122,7 +122,7 @@ class TestBeanOverrideMetadataTests {
|
||||
|
||||
static class SampleOneOverride {
|
||||
|
||||
@TestBean(methodName = "message")
|
||||
@TestBean
|
||||
String message;
|
||||
|
||||
static String message() {
|
||||
@@ -133,7 +133,7 @@ class TestBeanOverrideMetadataTests {
|
||||
|
||||
static class SampleOneOverrideWithName {
|
||||
|
||||
@TestBean(name = "anotherBean", methodName = "message")
|
||||
@TestBean(name = "anotherBean")
|
||||
String message;
|
||||
|
||||
static String message() {
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.test.context.bean.override.convention;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -70,10 +69,8 @@ class TestBeanOverrideProcessorTests {
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.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""",
|
||||
clazz.getName(), returnType.getName(), List.of("example1", "example3"));
|
||||
.withMessage("No static method found named example1() or example3() in %s with return type %s",
|
||||
MethodConventionTestCase.class.getName(), ExampleService.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,10 +80,8 @@ class TestBeanOverrideProcessorTests {
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.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(
|
||||
2, clazz.getName(), returnType.getName(), List.of("example2", "example4")));
|
||||
.withMessage("Found 2 competing static methods named example2() or example4() in %s with return type %s",
|
||||
clazz.getName(), returnType.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -106,10 +101,8 @@ class TestBeanOverrideProcessorTests {
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.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""",
|
||||
clazz.getName(), returnType.getName(), List.of("explicit1"));
|
||||
.withMessage("No static method found named explicit1() in %s with return type %s",
|
||||
clazz.getName(), returnType.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -133,10 +126,8 @@ class TestBeanOverrideProcessorTests {
|
||||
|
||||
assertThatIllegalStateException().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""",
|
||||
clazz.getName(), returnType.getName(), List.of("fieldTestOverride", "someFieldTestOverride"));
|
||||
.withMessage("No static method found named field() or someField() in %s with return type %s",
|
||||
clazz.getName(), returnType.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user