From c227fbfdf27d6ef28af66e1d0cc78fcc6a009a13 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 13 May 2023 19:26:54 +0200 Subject: [PATCH] Reorganize helper methods to align with first usage principle --- .../junit/jupiter/SpringExtension.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java index f4aaa32f0c..f2ecd59964 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java @@ -151,6 +151,32 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes getTestContextManager(context).prepareTestInstance(testInstance); } + /** + * Validate that test methods and test lifecycle methods in the supplied + * test class are not annotated with {@link Autowired @Autowired}. + * @since 5.3.2 + */ + private void validateAutowiredConfig(ExtensionContext context) { + // We save the result in the ExtensionContext.Store so that we don't + // re-validate all methods for the same test class multiple times. + Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE); + + String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> { + Method[] methodsWithErrors = + ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter); + return (methodsWithErrors.length == 0 ? NO_VIOLATIONS_DETECTED : + String.format( + "Test methods and test lifecycle methods must not be annotated with @Autowired. " + + "You should instead annotate individual method parameters with @Autowired, " + + "@Qualifier, or @Value. Offending methods in test class %s: %s", + testClass.getName(), Arrays.toString(methodsWithErrors))); + }, String.class); + + if (errorMessage != NO_VIOLATIONS_DETECTED) { + throw new IllegalStateException(errorMessage); + } + } + /** * Validate that the test class or its enclosing class doesn't attempt to record * application events in a parallel mode that makes it non-deterministic @@ -160,7 +186,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes */ private void validateRecordApplicationEventsConfig(ExtensionContext context) { // We save the result in the ExtensionContext.Store so that we don't - // re-validate all methods for the same test class multiple times. + // re-validate the configuration for the same test class multiple times. Store store = context.getStore(RECORD_APPLICATION_EVENTS_VALIDATION_NAMESPACE); String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> { @@ -190,32 +216,6 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes } } - /** - * Validate that test methods and test lifecycle methods in the supplied - * test class are not annotated with {@link Autowired @Autowired}. - * @since 5.3.2 - */ - private void validateAutowiredConfig(ExtensionContext context) { - // We save the result in the ExtensionContext.Store so that we don't - // re-validate all methods for the same test class multiple times. - Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE); - - String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> { - Method[] methodsWithErrors = - ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter); - return (methodsWithErrors.length == 0 ? NO_VIOLATIONS_DETECTED : - String.format( - "Test methods and test lifecycle methods must not be annotated with @Autowired. " + - "You should instead annotate individual method parameters with @Autowired, " + - "@Qualifier, or @Value. Offending methods in test class %s: %s", - testClass.getName(), Arrays.toString(methodsWithErrors))); - }, String.class); - - if (errorMessage != NO_VIOLATIONS_DETECTED) { - throw new IllegalStateException(errorMessage); - } - } - /** * Delegates to {@link TestContextManager#beforeTestMethod}. */