From b13963ee81a7aab4360f7e9e91c226de721aa6ea Mon Sep 17 00:00:00 2001
From: Sam Brannen <104798+sbrannen@users.noreply.github.com>
Date: Fri, 12 Apr 2024 14:19:48 +0200
Subject: [PATCH] =?UTF-8?q?Reject=20private=20JUnit=20Jupiter=20lifecycle?=
=?UTF-8?q?=20methods=20annotated=20with=20@=E2=81=A0Autowired?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes gh-32627
---
.../junit/jupiter/SpringExtension.java | 7 ++--
...edConfigurationErrorsIntegrationTests.java | 35 ++++++++++++++++++-
src/checkstyle/checkstyle-suppressions.xml | 2 +-
3 files changed, 37 insertions(+), 7 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 efc6d139a90..5e66aa58f4c 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
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2023 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
@@ -118,9 +117,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
List.of(BeforeAll.class, AfterAll.class, BeforeEach.class, AfterEach.class, Testable.class);
private static final MethodFilter autowiredTestOrLifecycleMethodFilter =
- ReflectionUtils.USER_DECLARED_METHODS
- .and(method -> !Modifier.isPrivate(method.getModifiers()))
- .and(SpringExtension::isAutowiredTestOrLifecycleMethod);
+ ReflectionUtils.USER_DECLARED_METHODS.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
/**
diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/AutowiredConfigurationErrorsIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/AutowiredConfigurationErrorsIntegrationTests.java
index 919e0b841b3..11fa6eb0fa4 100644
--- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/AutowiredConfigurationErrorsIntegrationTests.java
+++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/AutowiredConfigurationErrorsIntegrationTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2020 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,9 +65,11 @@ class AutowiredConfigurationErrorsIntegrationTests {
@ParameterizedTest
@ValueSource(classes = {
StaticAutowiredBeforeAllMethod.class,
+ StaticAutowiredPrivateBeforeAllMethod.class,
StaticAutowiredAfterAllMethod.class,
AutowiredBeforeEachMethod.class,
AutowiredAfterEachMethod.class,
+ AutowiredPrivateAfterEachMethod.class,
AutowiredTestMethod.class,
AutowiredRepeatedTestMethod.class,
AutowiredParameterizedTestMethod.class
@@ -166,6 +168,21 @@ class AutowiredConfigurationErrorsIntegrationTests {
}
}
+ @SpringJUnitConfig(Config.class)
+ @FailingTestCase
+ static class StaticAutowiredPrivateBeforeAllMethod {
+
+ @Autowired
+ @BeforeAll
+ private static void beforeAll(TestInfo testInfo) {
+ }
+
+ @Test
+ @DisplayName(DISPLAY_NAME)
+ void test() {
+ }
+ }
+
@SpringJUnitConfig(Config.class)
@TestInstance(PER_CLASS)
@FailingTestCase
@@ -243,6 +260,22 @@ class AutowiredConfigurationErrorsIntegrationTests {
}
}
+ @SpringJUnitConfig(Config.class)
+ @FailingTestCase
+ static class AutowiredPrivateAfterEachMethod {
+
+ @Test
+ @DisplayName(DISPLAY_NAME)
+ void test() {
+ }
+
+ @Autowired
+ @AfterEach
+ private void afterEach(TestInfo testInfo) {
+ }
+ }
+
+
@SpringJUnitConfig(Config.class)
@FailingTestCase
static class AutowiredTestMethod {
diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml
index f13676e5139..06360a35221 100644
--- a/src/checkstyle/checkstyle-suppressions.xml
+++ b/src/checkstyle/checkstyle-suppressions.xml
@@ -107,7 +107,7 @@
-
+