Skip to content

Commit

Permalink
Reorganize helper methods to align with first usage principle
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 13, 2023
1 parent f3f3dc6 commit c227fbf
Showing 1 changed file with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
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
Expand All @@ -160,7 +186,7 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
*/
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 -> {
Expand Down Expand Up @@ -190,32 +216,6 @@ private void validateRecordApplicationEventsConfig(ExtensionContext context) {
}
}

/**
* 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}.
*/
Expand Down

0 comments on commit c227fbf

Please sign in to comment.