Skip to content

Commit c227fbf

Browse files
committed
Reorganize helper methods to align with first usage principle
1 parent f3f3dc6 commit c227fbf

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java

+27-27
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
151151
getTestContextManager(context).prepareTestInstance(testInstance);
152152
}
153153

154+
/**
155+
* Validate that test methods and test lifecycle methods in the supplied
156+
* test class are not annotated with {@link Autowired @Autowired}.
157+
* @since 5.3.2
158+
*/
159+
private void validateAutowiredConfig(ExtensionContext context) {
160+
// We save the result in the ExtensionContext.Store so that we don't
161+
// re-validate all methods for the same test class multiple times.
162+
Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE);
163+
164+
String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> {
165+
Method[] methodsWithErrors =
166+
ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter);
167+
return (methodsWithErrors.length == 0 ? NO_VIOLATIONS_DETECTED :
168+
String.format(
169+
"Test methods and test lifecycle methods must not be annotated with @Autowired. " +
170+
"You should instead annotate individual method parameters with @Autowired, " +
171+
"@Qualifier, or @Value. Offending methods in test class %s: %s",
172+
testClass.getName(), Arrays.toString(methodsWithErrors)));
173+
}, String.class);
174+
175+
if (errorMessage != NO_VIOLATIONS_DETECTED) {
176+
throw new IllegalStateException(errorMessage);
177+
}
178+
}
179+
154180
/**
155181
* Validate that the test class or its enclosing class doesn't attempt to record
156182
* application events in a parallel mode that makes it non-deterministic
@@ -160,7 +186,7 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
160186
*/
161187
private void validateRecordApplicationEventsConfig(ExtensionContext context) {
162188
// We save the result in the ExtensionContext.Store so that we don't
163-
// re-validate all methods for the same test class multiple times.
189+
// re-validate the configuration for the same test class multiple times.
164190
Store store = context.getStore(RECORD_APPLICATION_EVENTS_VALIDATION_NAMESPACE);
165191

166192
String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> {
@@ -190,32 +216,6 @@ private void validateRecordApplicationEventsConfig(ExtensionContext context) {
190216
}
191217
}
192218

193-
/**
194-
* Validate that test methods and test lifecycle methods in the supplied
195-
* test class are not annotated with {@link Autowired @Autowired}.
196-
* @since 5.3.2
197-
*/
198-
private void validateAutowiredConfig(ExtensionContext context) {
199-
// We save the result in the ExtensionContext.Store so that we don't
200-
// re-validate all methods for the same test class multiple times.
201-
Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE);
202-
203-
String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> {
204-
Method[] methodsWithErrors =
205-
ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter);
206-
return (methodsWithErrors.length == 0 ? NO_VIOLATIONS_DETECTED :
207-
String.format(
208-
"Test methods and test lifecycle methods must not be annotated with @Autowired. " +
209-
"You should instead annotate individual method parameters with @Autowired, " +
210-
"@Qualifier, or @Value. Offending methods in test class %s: %s",
211-
testClass.getName(), Arrays.toString(methodsWithErrors)));
212-
}, String.class);
213-
214-
if (errorMessage != NO_VIOLATIONS_DETECTED) {
215-
throw new IllegalStateException(errorMessage);
216-
}
217-
}
218-
219219
/**
220220
* Delegates to {@link TestContextManager#beforeTestMethod}.
221221
*/

0 commit comments

Comments
 (0)