Browse Source

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.
pull/30915/head
Sam Brannen 2 years ago
parent
commit
07fe8eea83
  1. 8
      spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java

8
spring-test/src/main/java/org/springframework/test/context/observation/MicrometerObservationRegistryTestExecutionListener.java

@ -16,6 +16,8 @@
package org.springframework.test.context.observation; package org.springframework.test.context.observation;
import java.lang.reflect.Method;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -25,6 +27,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.util.ReflectionUtils;
/** /**
* {@code TestExecutionListener} which provides support for Micrometer's * {@code TestExecutionListener} which provides support for Micrometer's
@ -80,7 +83,10 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe
Class.forName(classToCheck, false, classLoader); Class.forName(classToCheck, false, classLoader);
classToCheck = OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME; classToCheck = OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME;
Class<?> clazz = Class.forName(classToCheck, false, classLoader); 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) { catch (Throwable ex) {
errorMessage = classToCheck + ". " + DEPENDENCIES_ERROR_MESSAGE; errorMessage = classToCheck + ". " + DEPENDENCIES_ERROR_MESSAGE;

Loading…
Cancel
Save