Reject private JUnit Jupiter lifecycle methods annotated with @⁠Autowired

Closes gh-32627
This commit is contained in:
Sam Brannen
2024-04-12 14:19:48 +02:00
parent 43cc18a5a1
commit b13963ee81
3 changed files with 37 additions and 7 deletions

View File

@@ -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);
/**

View File

@@ -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 {

View File

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