|
|
|
|
@ -89,8 +89,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -89,8 +89,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* {@link Namespace} in which {@code @Autowired} validation error messages |
|
|
|
|
* are stored, keyed by test class. |
|
|
|
|
*/ |
|
|
|
|
private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE = Namespace.create(SpringExtension.class.getName() + |
|
|
|
|
"#autowired.validation"); |
|
|
|
|
private static final Namespace AUTOWIRED_VALIDATION_NAMESPACE = |
|
|
|
|
Namespace.create(SpringExtension.class.getName() + "#autowired.validation"); |
|
|
|
|
|
|
|
|
|
private static final String NO_AUTOWIRED_VIOLATIONS_DETECTED = "NO AUTOWIRED VIOLATIONS DETECTED"; |
|
|
|
|
|
|
|
|
|
@ -101,8 +101,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -101,8 +101,8 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
|
|
|
|
|
private static final MethodFilter autowiredTestOrLifecycleMethodFilter = |
|
|
|
|
ReflectionUtils.USER_DECLARED_METHODS |
|
|
|
|
.and(method -> !Modifier.isPrivate(method.getModifiers())) |
|
|
|
|
.and(SpringExtension::isAutowiredTestOrLifecycleMethod); |
|
|
|
|
.and(method -> !Modifier.isPrivate(method.getModifiers())) |
|
|
|
|
.and(SpringExtension::isAutowiredTestOrLifecycleMethod); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -147,16 +147,16 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -147,16 +147,16 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
// 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 -> { |
|
|
|
|
|
|
|
|
|
String errorMessage = store.getOrComputeIfAbsent(context.getRequiredTestClass(), testClass -> { |
|
|
|
|
Method[] methodsWithErrors = |
|
|
|
|
ReflectionUtils.getUniqueDeclaredMethods(testClass, autowiredTestOrLifecycleMethodFilter); |
|
|
|
|
return (methodsWithErrors.length == 0 ? NO_AUTOWIRED_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))); |
|
|
|
|
"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_AUTOWIRED_VIOLATIONS_DETECTED) { |
|
|
|
|
@ -247,7 +247,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -247,7 +247,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
private boolean supportsApplicationEvents(ParameterContext parameterContext) { |
|
|
|
|
if (ApplicationEvents.class.isAssignableFrom(parameterContext.getParameter().getType())) { |
|
|
|
|
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 false; |
|
|
|
|
|