Skip to content

Commit b13963e

Browse files
committed
Reject private JUnit Jupiter lifecycle methods annotated with @⁠Autowired
Closes gh-32627
1 parent 43cc18a commit b13963e

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.lang.reflect.Constructor;
2121
import java.lang.reflect.Executable;
2222
import java.lang.reflect.Method;
23-
import java.lang.reflect.Modifier;
2423
import java.lang.reflect.Parameter;
2524
import java.util.Arrays;
2625
import java.util.List;
@@ -118,9 +117,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
118117
List.of(BeforeAll.class, AfterAll.class, BeforeEach.class, AfterEach.class, Testable.class);
119118

120119
private static final MethodFilter autowiredTestOrLifecycleMethodFilter =
121-
ReflectionUtils.USER_DECLARED_METHODS
122-
.and(method -> !Modifier.isPrivate(method.getModifiers()))
123-
.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
120+
ReflectionUtils.USER_DECLARED_METHODS.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
124121

125122

126123
/**

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,9 +65,11 @@ class AutowiredConfigurationErrorsIntegrationTests {
6565
@ParameterizedTest
6666
@ValueSource(classes = {
6767
StaticAutowiredBeforeAllMethod.class,
68+
StaticAutowiredPrivateBeforeAllMethod.class,
6869
StaticAutowiredAfterAllMethod.class,
6970
AutowiredBeforeEachMethod.class,
7071
AutowiredAfterEachMethod.class,
72+
AutowiredPrivateAfterEachMethod.class,
7173
AutowiredTestMethod.class,
7274
AutowiredRepeatedTestMethod.class,
7375
AutowiredParameterizedTestMethod.class
@@ -166,6 +168,21 @@ void test() {
166168
}
167169
}
168170

171+
@SpringJUnitConfig(Config.class)
172+
@FailingTestCase
173+
static class StaticAutowiredPrivateBeforeAllMethod {
174+
175+
@Autowired
176+
@BeforeAll
177+
private static void beforeAll(TestInfo testInfo) {
178+
}
179+
180+
@Test
181+
@DisplayName(DISPLAY_NAME)
182+
void test() {
183+
}
184+
}
185+
169186
@SpringJUnitConfig(Config.class)
170187
@TestInstance(PER_CLASS)
171188
@FailingTestCase
@@ -243,6 +260,22 @@ void afterEach(TestInfo testInfo) {
243260
}
244261
}
245262

263+
@SpringJUnitConfig(Config.class)
264+
@FailingTestCase
265+
static class AutowiredPrivateAfterEachMethod {
266+
267+
@Test
268+
@DisplayName(DISPLAY_NAME)
269+
void test() {
270+
}
271+
272+
@Autowired
273+
@AfterEach
274+
private void afterEach(TestInfo testInfo) {
275+
}
276+
}
277+
278+
246279
@SpringJUnitConfig(Config.class)
247280
@FailingTestCase
248281
static class AutowiredTestMethod {

src/checkstyle/checkstyle-suppressions.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]util[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
108108
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]web[\\/](client|reactive|servlet)[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
109109
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]context[\\/](aot|junit4)" checks="SpringJUnit5"/>
110-
<suppress files="ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests" checks="SpringJUnit5"/>
110+
<suppress files="AutowiredConfigurationErrorsIntegrationTests|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests" checks="SpringJUnit5"/>
111111
<suppress files=".+TestSuite|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests" checks="IllegalImport" id="bannedJUnit4Imports"/>
112112
<suppress files="org[\\/]springframework[\\/]test[\\/]context[\\/].+[\\/](ExpectedExceptionSpringRunnerTests|StandardJUnit4FeaturesTests|ProgrammaticTxMgmtTestNGTests)" checks="RegexpSinglelineJava" id="expectedExceptionAnnotation"/>
113113

0 commit comments

Comments
 (0)