mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Use MeterRegistry.isClosed() for tests
Closes gh-14975
This commit is contained in:
parent
974f90cefe
commit
62ca9e47b8
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.atlas;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.spectator.atlas.AtlasConfig;
|
||||
import io.micrometer.atlas.AtlasMeterRegistry;
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link AtlasMetricsExportAutoConfiguration}.
|
||||
@ -89,27 +83,14 @@ public class AtlasMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
AtlasMeterRegistry registry = spyOnDisposableBean(
|
||||
AtlasMeterRegistry.class, context);
|
||||
AtlasMeterRegistry registry = context
|
||||
.getBean(AtlasMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.datadog;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.datadog.DatadogConfig;
|
||||
import io.micrometer.datadog.DatadogMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link DatadogMetricsExportAutoConfiguration}.
|
||||
@ -98,27 +92,14 @@ public class DatadogMetricsExportAutoConfigurationTests {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.withPropertyValues("management.metrics.export.datadog.api-key=abcde")
|
||||
.run((context) -> {
|
||||
DatadogMeterRegistry registry = spyOnDisposableBean(
|
||||
DatadogMeterRegistry.class, context);
|
||||
DatadogMeterRegistry registry = context
|
||||
.getBean(DatadogMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.ganglia.GangliaConfig;
|
||||
import io.micrometer.ganglia.GangliaMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link GangliaMetricsExportAutoConfiguration}.
|
||||
@ -89,27 +83,14 @@ public class GangliaMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
GangliaMeterRegistry registry = spyOnDisposableBean(
|
||||
GangliaMeterRegistry.class, context);
|
||||
GangliaMeterRegistry registry = context
|
||||
.getBean(GangliaMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.graphite;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import io.micrometer.graphite.GraphiteConfig;
|
||||
@ -25,16 +23,12 @@ import io.micrometer.graphite.GraphiteMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link GraphiteMetricsExportAutoConfiguration}.
|
||||
@ -106,27 +100,14 @@ public class GraphiteMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
GraphiteMeterRegistry registry = spyOnDisposableBean(
|
||||
GraphiteMeterRegistry.class, context);
|
||||
GraphiteMeterRegistry registry = context
|
||||
.getBean(GraphiteMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.influx.InfluxConfig;
|
||||
import io.micrometer.influx.InfluxMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link InfluxMetricsExportAutoConfiguration}.
|
||||
@ -89,27 +83,14 @@ public class InfluxMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
InfluxMeterRegistry registry = spyOnDisposableBean(
|
||||
InfluxMeterRegistry.class, context);
|
||||
InfluxMeterRegistry registry = context
|
||||
.getBean(InfluxMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.jmx;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.jmx.JmxConfig;
|
||||
import io.micrometer.jmx.JmxMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link JmxMetricsExportAutoConfiguration}.
|
||||
@ -89,27 +83,13 @@ public class JmxMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
JmxMeterRegistry registry = spyOnDisposableBean(
|
||||
JmxMeterRegistry.class, context);
|
||||
JmxMeterRegistry registry = context.getBean(JmxMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.newrelic.NewRelicConfig;
|
||||
import io.micrometer.newrelic.NewRelicMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -111,27 +105,14 @@ public class NewRelicMetricsExportAutoConfigurationTests {
|
||||
.withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
|
||||
"management.metrics.export.newrelic.account-id=abcde")
|
||||
.withUserConfiguration(BaseConfiguration.class).run((context) -> {
|
||||
NewRelicMeterRegistry registry = spyOnDisposableBean(
|
||||
NewRelicMeterRegistry.class, context);
|
||||
NewRelicMeterRegistry registry = context
|
||||
.getBean(NewRelicMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.signalfx.SignalFxConfig;
|
||||
import io.micrometer.signalfx.SignalFxMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link SignalFxMetricsExportAutoConfiguration}.
|
||||
@ -106,27 +100,14 @@ public class SignalFxMetricsExportAutoConfigurationTests {
|
||||
.withPropertyValues(
|
||||
"management.metrics.export.signalfx.access-token=abcde")
|
||||
.withUserConfiguration(BaseConfiguration.class).run((context) -> {
|
||||
SignalFxMeterRegistry registry = spyOnDisposableBean(
|
||||
SignalFxMeterRegistry.class, context);
|
||||
SignalFxMeterRegistry registry = context
|
||||
.getBean(SignalFxMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.statsd;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.statsd.StatsdConfig;
|
||||
import io.micrometer.statsd.StatsdMeterRegistry;
|
||||
@ -25,16 +23,12 @@ import io.micrometer.statsd.StatsdMetrics;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link StatsdMetricsExportAutoConfiguration}.
|
||||
@ -91,27 +85,14 @@ public class StatsdMetricsExportAutoConfigurationTests {
|
||||
public void stopsMeterRegistryWhenContextIsClosed() {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.run((context) -> {
|
||||
StatsdMeterRegistry registry = spyOnDisposableBean(
|
||||
StatsdMeterRegistry.class, context);
|
||||
StatsdMeterRegistry registry = context
|
||||
.getBean(StatsdMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
@ -16,24 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.micrometer.core.instrument.Clock;
|
||||
import io.micrometer.wavefront.WavefrontConfig;
|
||||
import io.micrometer.wavefront.WavefrontMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link WavefrontMetricsExportAutoConfiguration}.
|
||||
@ -90,27 +84,14 @@ public class WavefrontMetricsExportAutoConfigurationTests {
|
||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||
.withPropertyValues("management.metrics.export.wavefront.api-token=abcde")
|
||||
.run((context) -> {
|
||||
WavefrontMeterRegistry registry = spyOnDisposableBean(
|
||||
WavefrontMeterRegistry.class, context);
|
||||
WavefrontMeterRegistry registry = context
|
||||
.getBean(WavefrontMeterRegistry.class);
|
||||
assertThat(registry.isClosed()).isFalse();
|
||||
context.close();
|
||||
verify(registry).stop();
|
||||
assertThat(registry.isClosed()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T spyOnDisposableBean(Class<T> type,
|
||||
AssertableApplicationContext context) {
|
||||
String[] names = context.getBeanNamesForType(type);
|
||||
assertThat(names).hasSize(1);
|
||||
String registryBeanName = names[0];
|
||||
Map<String, Object> disposableBeans = (Map<String, Object>) ReflectionTestUtils
|
||||
.getField(context.getAutowireCapableBeanFactory(), "disposableBeans");
|
||||
Object registryAdapter = disposableBeans.get(registryBeanName);
|
||||
T registry = (T) spy(ReflectionTestUtils.getField(registryAdapter, "bean"));
|
||||
ReflectionTestUtils.setField(registryAdapter, "bean", registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BaseConfiguration {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user