Browse Source

Reject private JUnit Jupiter lifecycle methods annotated with @⁠Autowired

Closes gh-32627
pull/32635/head
Sam Brannen 2 years ago
parent
commit
b13963ee81
  1. 7
      spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java
  2. 35
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/AutowiredConfigurationErrorsIntegrationTests.java
  3. 2
      src/checkstyle/checkstyle-suppressions.xml

7
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Constructor;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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); List.of(BeforeAll.class, AfterAll.class, BeforeEach.class, AfterEach.class, Testable.class);
private static final MethodFilter autowiredTestOrLifecycleMethodFilter = private static final MethodFilter autowiredTestOrLifecycleMethodFilter =
ReflectionUtils.USER_DECLARED_METHODS ReflectionUtils.USER_DECLARED_METHODS.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
.and(method -> !Modifier.isPrivate(method.getModifiers()))
.and(SpringExtension::isAutowiredTestOrLifecycleMethod);
/** /**

35
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,9 +65,11 @@ class AutowiredConfigurationErrorsIntegrationTests {
@ParameterizedTest @ParameterizedTest
@ValueSource(classes = { @ValueSource(classes = {
StaticAutowiredBeforeAllMethod.class, StaticAutowiredBeforeAllMethod.class,
StaticAutowiredPrivateBeforeAllMethod.class,
StaticAutowiredAfterAllMethod.class, StaticAutowiredAfterAllMethod.class,
AutowiredBeforeEachMethod.class, AutowiredBeforeEachMethod.class,
AutowiredAfterEachMethod.class, AutowiredAfterEachMethod.class,
AutowiredPrivateAfterEachMethod.class,
AutowiredTestMethod.class, AutowiredTestMethod.class,
AutowiredRepeatedTestMethod.class, AutowiredRepeatedTestMethod.class,
AutowiredParameterizedTestMethod.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) @SpringJUnitConfig(Config.class)
@TestInstance(PER_CLASS) @TestInstance(PER_CLASS)
@FailingTestCase @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) @SpringJUnitConfig(Config.class)
@FailingTestCase @FailingTestCase
static class AutowiredTestMethod { static class AutowiredTestMethod {

2
src/checkstyle/checkstyle-suppressions.xml

@ -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[\\/]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[\\/]web[\\/](client|reactive|servlet)[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]context[\\/](aot|junit4)" checks="SpringJUnit5"/> <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=".+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"/> <suppress files="org[\\/]springframework[\\/]test[\\/]context[\\/].+[\\/](ExpectedExceptionSpringRunnerTests|StandardJUnit4FeaturesTests|ProgrammaticTxMgmtTestNGTests)" checks="RegexpSinglelineJava" id="expectedExceptionAnnotation"/>

Loading…
Cancel
Save