Browse Source

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
pull/32999/head
Stéphane Nicoll 2 years ago
parent
commit
96302a7102
  1. 42
      framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc
  2. 29
      spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java
  3. 31
      spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java
  4. 4
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/AbstractTestBeanIntegrationTestCase.java
  5. 4
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/FailingTestBeanByTypeIntegrationTests.java
  6. 18
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/FailingTestBeanInheritanceIntegrationTests.java
  7. 28
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/FailingTestBeanIntegrationTests.java
  8. 2
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanByTypeIntegrationTests.java
  9. 6
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanInheritanceIntegrationTests.java
  10. 12
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanIntegrationTests.java
  11. 8
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideMetadataTests.java
  12. 25
      spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessorTests.java

42
framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc

@ -5,19 +5,19 @@ @@ -5,19 +5,19 @@
`ApplicationContext` with an instance provided by a conventionally named static factory
method.
By default, the associated factory method name is derived from the annotated field's name,
but the annotation allows for a specific method name to be provided.
The associated factory method name is derived from the annotated field's name, or bean
name if specified. A `static` method with no argument that returns a type compatible
with the type of the bean to override is expected. To make things more explicit, or if
you'd rather use a different name, the annotation allows for a specific method name to
be provided.
The `@TestBean` annotation uses the `REPLACE_DEFINITION`
xref:testing/testcontext-framework/bean-overriding.adoc#testcontext-bean-overriding-custom[strategy for test bean overriding].
By default, the annotated field's type is used to search for candidate definitions to override.
In that case it is required that exactly one definition matches, but note that `@Qualifier`
annotations are also taken into account for the purpose of matching.
Users can also make things entirely explicit by specifying a bean `name` in the annotation.
The following example shows how to fully configure the `@TestBean` annotation, with
explicit values equivalent to the defaults:
The following example shows how to use the default behavior of the `@TestBean` annotation:
[tabs]
======
@ -26,17 +26,41 @@ Java:: @@ -26,17 +26,41 @@ Java::
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
class OverrideBeanTests {
@TestBean(name = "service", methodName = "serviceTestOverride") // <1>
@TestBean // <1>
private CustomService service;
// test case body...
private static CustomService serviceTestOverride() { // <2>
private static CustomService service() { // <2>
return new MyFakeCustomService();
}
}
----
<1> Mark a field for bean overriding in this test class.
<1> Mark a field for overriding of the bean with type `CustomService`.
<2> The result of this static method will be used as the instance and injected into the field.
======
The following example shows how to fully configure the `@TestBean` annotation:
[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
class OverrideBeanTests {
@TestBean(name = "service", methodName = "createCustomService") // <1>
private CustomService service;
// test case body...
private static CustomService createCustomService() { // <2>
return new MyFakeCustomService();
}
}
----
<1> Mark a field for overriding of the bean with name `service`.
<2> The result of this static method will be used as the instance and injected into the field.
======

29
spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java

@ -43,9 +43,9 @@ import org.springframework.test.context.bean.override.BeanOverride; @@ -43,9 +43,9 @@ import org.springframework.test.context.bean.override.BeanOverride;
* <ul>
* <li>If the {@link #methodName()} is specified, look for a static method with
* that name.</li>
* <li>If a method name is not specified, look for exactly one static method named
* with a suffix equal to {@value #CONVENTION_SUFFIX} and starting with either the
* name of the annotated field or the name of the bean (if specified).</li>
* <li>If a method name is not specified, look for exactly one static method
* named with either the name of the annotated field or the name of the bean
* (if specified).</li>
* </ul>
*
* <p>Consider the following example.
@ -58,16 +58,16 @@ import org.springframework.test.context.bean.override.BeanOverride; @@ -58,16 +58,16 @@ import org.springframework.test.context.bean.override.BeanOverride;
*
* // Tests
*
* private static CustomerRepository repositoryTestOverride() {
* private static CustomerRepository repository() {
* return new TestCustomerRepository();
* }
* }</code></pre>
*
* <p>In the example above, the {@code repository} bean is replaced by the
* instance generated by the {@code repositoryTestOverride()} method. Not only
* is the overridden instance injected into the {@code repository} field, but it
* is also replaced in the {@code BeanFactory} so that other injection points
* for that bean use the overridden bean instance.
* instance generated by the {@code repository()} method. Not only is the
* overridden instance injected into the {@code repository} field, but it is
* also replaced in the {@code BeanFactory} so that other injection points for
* that bean use the overridden bean instance.
*
* <p>To make things more explicit, the bean and method names can be set,
* as shown in the following example.
@ -75,7 +75,7 @@ import org.springframework.test.context.bean.override.BeanOverride; @@ -75,7 +75,7 @@ import org.springframework.test.context.bean.override.BeanOverride;
* <pre><code>
* class CustomerServiceTests {
*
* &#064;TestBean(name = "repository", methodName = "createTestCustomerRepository")
* &#064;TestBean(name = "customerRepository", methodName = "createTestCustomerRepository")
* private CustomerRepository repository;
*
* // Tests
@ -97,14 +97,6 @@ import org.springframework.test.context.bean.override.BeanOverride; @@ -97,14 +97,6 @@ import org.springframework.test.context.bean.override.BeanOverride;
@BeanOverride(TestBeanOverrideProcessor.class)
public @interface TestBean {
/**
* Required suffix for the name of a factory method that overrides a bean
* instance when the factory method is detected by convention.
* @see #methodName()
*/
String CONVENTION_SUFFIX = "TestOverride";
/**
* Alias for {@link #name()}.
* <p>Intended to be used when no other attributes are needed &mdash; for
@ -130,8 +122,7 @@ public @interface TestBean { @@ -130,8 +122,7 @@ public @interface TestBean {
* also considered. Similarly, in case the test class inherits from a base
* class the whole class hierarchy is considered.
* <p>If left unspecified, the name of the factory method will be detected
* based on convention.
* @see #CONVENTION_SUFFIX
* based either on the name of the annotated field or the name of the bean.
*/
String methodName() default "";

31
spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBeanOverrideProcessor.java

@ -21,9 +21,11 @@ import java.lang.reflect.Field; @@ -21,9 +21,11 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.ResolvableType;
@ -41,6 +43,7 @@ import org.springframework.util.StringUtils; @@ -41,6 +43,7 @@ import org.springframework.util.StringUtils;
*
* @author Simon Baslé
* @author Sam Brannen
* @author Stephane Nicoll
* @since 6.2
*/
class TestBeanOverrideProcessor implements BeanOverrideProcessor {
@ -58,14 +61,14 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor { @@ -58,14 +61,14 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
overrideMethod = findTestBeanFactoryMethod(testClass, field.getType(), methodName);
}
else {
// Otherwise, search for candidate factory methods using the convention
// suffix and the field name or explicit bean name (if any).
// Otherwise, search for candidate factory methods the field name
// or explicit bean name (if any).
List<String> candidateMethodNames = new ArrayList<>();
candidateMethodNames.add(field.getName() + TestBean.CONVENTION_SUFFIX);
candidateMethodNames.add(field.getName());
String beanName = testBeanAnnotation.name();
if (StringUtils.hasText(beanName)) {
candidateMethodNames.add(beanName + TestBean.CONVENTION_SUFFIX);
candidateMethodNames.add(beanName);
}
overrideMethod = findTestBeanFactoryMethod(testClass, field.getType(), candidateMethodNames);
}
@ -75,7 +78,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor { @@ -75,7 +78,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
/**
* Find a test bean factory {@link Method} for the given {@link Class}.
* <p>Delegates to {@link #findTestBeanFactoryMethod(Class, Class, List)}.
* <p>Delegates to {@link #findTestBeanFactoryMethod(Class, Class, Collection)}.
*/
Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, String... methodNames) {
return findTestBeanFactoryMethod(clazz, methodReturnType, List.of(methodNames));
@ -104,7 +107,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor { @@ -104,7 +107,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
* @throws IllegalStateException if a matching factory method cannot
* be found or multiple methods match
*/
Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, List<String> methodNames) {
Method findTestBeanFactoryMethod(Class<?> clazz, Class<?> methodReturnType, Collection<String> methodNames) {
Assert.notEmpty(methodNames, "At least one candidate method name is required");
Set<String> supportedNames = new LinkedHashSet<>(methodNames);
MethodFilter methodFilter = method -> (Modifier.isStatic(method.getModifiers()) &&
@ -113,16 +116,16 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor { @@ -113,16 +116,16 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
Set<Method> methods = findMethods(clazz, methodFilter);
Assert.state(!methods.isEmpty(), () -> """
Failed to find a static test bean factory method in %s with return type %s \
whose name matches one of the supported candidates %s""".formatted(
clazz.getName(), methodReturnType.getName(), supportedNames));
String methodNamesDescription = supportedNames.stream()
.map(name -> name + "()").collect(Collectors.joining(" or "));
Assert.state(!methods.isEmpty(), () ->
"No static method found named %s in %s with return type %s".formatted(
methodNamesDescription, clazz.getName(), methodReturnType.getName()));
long uniqueMethodNameCount = methods.stream().map(Method::getName).distinct().count();
Assert.state(uniqueMethodNameCount == 1, () -> """
Found %d competing static test bean factory methods in %s with return type %s \
whose name matches one of the supported candidates %s""".formatted(
uniqueMethodNameCount, clazz.getName(), methodReturnType.getName(), supportedNames));
Assert.state(uniqueMethodNameCount == 1, () ->
"Found %d competing static methods named %s in %s with return type %s".formatted(
uniqueMethodNameCount, methodNamesDescription, clazz.getName(), methodReturnType.getName()));
return methods.iterator().next();
}

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

@ -32,11 +32,11 @@ abstract class AbstractTestBeanIntegrationTestCase { @@ -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");
}

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

@ -80,7 +80,7 @@ class FailingTestBeanByTypeIntegrationTests { @@ -80,7 +80,7 @@ class FailingTestBeanByTypeIntegrationTests {
void test() {
}
static ExampleService exampleTestOverride() {
static ExampleService example() {
return fail("unexpected override");
}
@ -100,7 +100,7 @@ class FailingTestBeanByTypeIntegrationTests { @@ -100,7 +100,7 @@ class FailingTestBeanByTypeIntegrationTests {
void test() {
}
static ExampleService exampleTestOverride() {
static ExampleService example() {
return fail("unexpected override");
}

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

@ -18,6 +18,7 @@ package org.springframework.test.context.bean.override.convention; @@ -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 { @@ -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 { @@ -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 { @@ -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");
}

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

@ -66,25 +66,21 @@ class FailingTestBeanIntegrationTests { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -167,7 +163,7 @@ class FailingTestBeanIntegrationTests {
fail("should fail earlier");
}
static String fieldTestOverride() {
static String field() {
return "should be ignored";
}

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

@ -51,7 +51,7 @@ public class TestBeanByTypeIntegrationTests { @@ -51,7 +51,7 @@ public class TestBeanByTypeIntegrationTests {
@CustomQualifier
StringBuilder anyNameForStringBuilder2;
static ExampleService anyNameForServiceTestOverride() {
static ExampleService anyNameForService() {
return new RealExampleService("Mocked greeting");
}

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

@ -57,12 +57,12 @@ public class TestBeanInheritanceIntegrationTests { @@ -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");
}

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

@ -50,17 +50,17 @@ public class TestBeanIntegrationTests { @@ -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 { @@ -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 { @@ -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

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

@ -54,8 +54,8 @@ class TestBeanOverrideMetadataTests { @@ -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 { @@ -122,7 +122,7 @@ class TestBeanOverrideMetadataTests {
static class SampleOneOverride {
@TestBean(methodName = "message")
@TestBean
String message;
static String message() {
@ -133,7 +133,7 @@ class TestBeanOverrideMetadataTests { @@ -133,7 +133,7 @@ class TestBeanOverrideMetadataTests {
static class SampleOneOverrideWithName {
@TestBean(name = "anotherBean", methodName = "message")
@TestBean(name = "anotherBean")
String message;
static String message() {

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

@ -18,7 +18,6 @@ package org.springframework.test.context.bean.override.convention; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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

Loading…
Cancel
Save