Skip to content

Commit 008de18

Browse files
committed
Use ReflectionTestUtils for invoking @DynamicPropertySource methods
1 parent a68a884 commit 008de18

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/DynamicPropertySourceMethodsImporter.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.javapoet.CodeBlock;
4747
import org.springframework.test.context.DynamicPropertyRegistry;
4848
import org.springframework.test.context.DynamicPropertySource;
49+
import org.springframework.test.util.ReflectionTestUtils;
4950
import org.springframework.util.Assert;
5051
import org.springframework.util.ClassUtils;
5152
import org.springframework.util.ReflectionUtils;
@@ -201,7 +202,6 @@ private static GeneratedMethod generateMethod(GenerationContext generationContex
201202
code.addModifiers(javax.lang.model.element.Modifier.PRIVATE,
202203
javax.lang.model.element.Modifier.STATIC);
203204
code.addParameter(DynamicPropertyRegistry.class, DYNAMIC_PROPERTY_REGISTRY);
204-
205205
if (isMethodAccessible(generatedClass, method)) {
206206
code.addStatement(CodeBlock.of("$T.$L($L)", method.getDeclaringClass(), method.getName(),
207207
DYNAMIC_PROPERTY_REGISTRY));
@@ -211,12 +211,9 @@ private static GeneratedMethod generateMethod(GenerationContext generationContex
211211
code.beginControlFlow("try");
212212
code.addStatement("$T<?> clazz = $T.forName($S, $T.class.getClassLoader())", Class.class,
213213
ClassUtils.class, ClassName.get(method.getDeclaringClass()), generatedClass.getName());
214-
code.addStatement("$T method = $T.findMethod(clazz, $S, $T.class)", Method.class,
215-
ReflectionUtils.class, method.getName(), DynamicPropertyRegistry.class);
216-
code.addStatement("$T.notNull(method, $S)", Assert.class,
217-
"Method '" + method.getName() + "' is not found");
218-
code.addStatement("$T.makeAccessible(method)", ReflectionUtils.class);
219-
code.addStatement("$T.invokeMethod(method, null, $L)", ReflectionUtils.class,
214+
// ReflectionTestUtils can be used here because
215+
// @DynamicPropertyRegistry in a test module.
216+
code.addStatement("$T.invokeMethod(clazz, $S, $L)", ReflectionTestUtils.class, method.getName(),
220217
DYNAMIC_PROPERTY_REGISTRY);
221218
code.nextControlFlow("catch ($T ex)", ClassNotFoundException.class);
222219
code.addStatement("throw new $T(ex)", RuntimeException.class);

0 commit comments

Comments
 (0)