Polishing

This commit is contained in:
Juergen Hoeller 2021-09-30 17:34:22 +02:00
parent a295a28e4b
commit 4f44ae3f28
1 changed files with 11 additions and 11 deletions

View File

@ -89,8 +89,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
* {@link Namespace} in which {@code @Autowired} validation error messages * {@link Namespace} in which {@code @Autowired} validation error messages
* are stored, keyed by test class. * are stored, keyed by test class.
*/ */
private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE = Namespace.create(SpringExtension.class.getName() + private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE =
"#autowired.validation"); Namespace.create(SpringExtension.class.getName() + "#autowired.validation");
private static final String NO_AUTOWIRED_VIOLATIONS_DETECTED = "NO AUTOWIRED VIOLATIONS DETECTED"; private static final String NO_AUTOWIRED_VIOLATIONS_DETECTED = "NO AUTOWIRED VIOLATIONS DETECTED";
@ -101,8 +101,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
private static final MethodFilter autowiredTestOrLifecycleMethodFilter = private static final MethodFilter autowiredTestOrLifecycleMethodFilter =
ReflectionUtils.USER_DECLARED_METHODS ReflectionUtils.USER_DECLARED_METHODS
.and(method -> !Modifier.isPrivate(method.getModifiers())) .and(method -> !Modifier.isPrivate(method.getModifiers()))
.and(SpringExtension::isAutowiredTestOrLifecycleMethod); .and(SpringExtension::isAutowiredTestOrLifecycleMethod);
/** /**
@ -147,16 +147,16 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
// We save the result in the ExtensionContext.Store so that we don't // 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 all methods for the same test class multiple times.
Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE); Store store = context.getStore(AUTOWIRED_VALIDATION_NAMESPACE);
String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(),
testClass -> { String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> {
Method[] methodsWithErrors = Method[] methodsWithErrors =
ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter); ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter);
return (methodsWithErrors.length == 0 ? NO_AUTOWIRED_VIOLATIONS_DETECTED : return (methodsWithErrors.length == 0 ? NO_AUTOWIRED_VIOLATIONS_DETECTED :
String.format( String.format(
"Test methods and test lifecycle methods must not be annotated with @Autowired. " + "Test methods and test lifecycle methods must not be annotated with @Autowired. " +
"You should instead annotate individual method parameters with @Autowired, " + "You should instead annotate individual method parameters with @Autowired, " +
"@Qualifier, or @Value. Offending methods in test class %s: %s", "@Qualifier, or @Value. Offending methods in test class %s: %s",
testClass.getName(), Arrays.toString(methodsWithErrors))); testClass.getName(), Arrays.toString(methodsWithErrors)));
}, String.class); }, String.class);
if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED) { if (errorMessage != NO_AUTOWIRED_VIOLATIONS_DETECTED) {
@ -247,7 +247,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
private boolean supportsApplicationEvents(ParameterContext parameterContext) { private boolean supportsApplicationEvents(ParameterContext parameterContext) {
if (ApplicationEvents.class.isAssignableFrom(parameterContext.getParameter().getType())) { if (ApplicationEvents.class.isAssignableFrom(parameterContext.getParameter().getType())) {
Assert.isTrue(parameterContext.getDeclaringExecutable() instanceof Method, Assert.isTrue(parameterContext.getDeclaringExecutable() instanceof Method,
"ApplicationEvents can only be injected into test and lifecycle methods"); "ApplicationEvents can only be injected into test and lifecycle methods");
return true; return true;
} }
return false; return false;