From cd67b285e1038323fd4e8434ec48e40190d63236 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 23 Nov 2018 16:30:24 +0100 Subject: [PATCH] Ensure that parameter resolution in SpringExtension is thread-safe Prior to this commit, parallel execution of @BeforeEach and @AfterEach methods that accepted @Autowired arguments would fail intermittently due to a race condition in the internal implementation of the JDK's java.lang.reflect.Executable.getParameters() method. This commit addresses this issue by creating instances of SynthesizingMethodParameter via SynthesizingMethodParameter.forExecutable(Executable, int) instead of SynthesizingMethodParameter.forParameter(Parameter), since the latter looks up the parameter index by iterating over the array returned by Executable.getParameters() (which is not thread-safe). Issue: SPR-17533 --- .../test/context/junit/jupiter/ParameterAutowireUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java index b6c9c101c14..b9351b94866 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java @@ -118,7 +118,8 @@ abstract class ParameterAutowireUtils { Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class); boolean required = (autowired == null || autowired.required()); - MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter); + MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable( + parameter.getDeclaringExecutable(), parameterIndex); DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required); descriptor.setContainingClass(containingClass); return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);