From 07fe8eea834cd156e2410ea430a14e1e8819661e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 3 Jul 2023 14:48:53 +0200 Subject: [PATCH] Improve error message for wrong version of micrometer-observation This commit also reverts to using ReflectionUtils.findMethod in order to make the check more robust in case the Micrometer team refactors the code base and declares the `getObservationRegistry()` method in a super type. --- ...icrometerObservationRegistryTestExecutionListener.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java index b4df831b494e..954e2452254b 100644 --- a/spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java @@ -16,6 +16,8 @@ package org.springframework.test.context.observation; +import java.lang.reflect.Method; + import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import org.apache.commons.logging.Log; @@ -25,6 +27,7 @@ import org.springframework.core.Conventions; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; +import org.springframework.util.ReflectionUtils; /** * {@code TestExecutionListener} which provides support for Micrometer's @@ -80,7 +83,10 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe Class.forName(classToCheck, false, classLoader); classToCheck = OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME; Class clazz = Class.forName(classToCheck, false, classLoader); - clazz.getMethod("getObservationRegistry"); + Method method = ReflectionUtils.findMethod(clazz, "getObservationRegistry"); + if (method == null) { + errorMessage = classToCheck + ". Method getObservationRegistry() not found. " + DEPENDENCIES_ERROR_MESSAGE; + } } catch (Throwable ex) { errorMessage = classToCheck + ". " + DEPENDENCIES_ERROR_MESSAGE;