Merge pull request #14975 from Christoph Dreis

* gh-14975:
  Use MeterRegistry.isClosed() for tests
pull/16246/head
Andy Wilkinson 6 years ago
commit b1d159b3e1

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.atlas; package org.springframework.boot.actuate.autoconfigure.metrics.export.atlas;
import java.util.Map;
import com.netflix.spectator.atlas.AtlasConfig; import com.netflix.spectator.atlas.AtlasConfig;
import io.micrometer.atlas.AtlasMeterRegistry; import io.micrometer.atlas.AtlasMeterRegistry;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link AtlasMetricsExportAutoConfiguration}. * Tests for {@link AtlasMetricsExportAutoConfiguration}.
@ -89,27 +83,14 @@ public class AtlasMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
AtlasMeterRegistry registry = spyOnDisposableBean( AtlasMeterRegistry registry = context
AtlasMeterRegistry.class, context); .getBean(AtlasMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.datadog; package org.springframework.boot.actuate.autoconfigure.metrics.export.datadog;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.datadog.DatadogConfig; import io.micrometer.datadog.DatadogConfig;
import io.micrometer.datadog.DatadogMeterRegistry; import io.micrometer.datadog.DatadogMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link DatadogMetricsExportAutoConfiguration}. * Tests for {@link DatadogMetricsExportAutoConfiguration}.
@ -98,27 +92,14 @@ public class DatadogMetricsExportAutoConfigurationTests {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.datadog.api-key=abcde") .withPropertyValues("management.metrics.export.datadog.api-key=abcde")
.run((context) -> { .run((context) -> {
DatadogMeterRegistry registry = spyOnDisposableBean( DatadogMeterRegistry registry = context
DatadogMeterRegistry.class, context); .getBean(DatadogMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia; package org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.ganglia.GangliaConfig; import io.micrometer.ganglia.GangliaConfig;
import io.micrometer.ganglia.GangliaMeterRegistry; import io.micrometer.ganglia.GangliaMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link GangliaMetricsExportAutoConfiguration}. * Tests for {@link GangliaMetricsExportAutoConfiguration}.
@ -89,27 +83,14 @@ public class GangliaMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
GangliaMeterRegistry registry = spyOnDisposableBean( GangliaMeterRegistry registry = context
GangliaMeterRegistry.class, context); .getBean(GangliaMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,8 +16,6 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.graphite; package org.springframework.boot.actuate.autoconfigure.metrics.export.graphite;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Tags; import io.micrometer.core.instrument.Tags;
import io.micrometer.graphite.GraphiteConfig; import io.micrometer.graphite.GraphiteConfig;
@ -25,16 +23,12 @@ import io.micrometer.graphite.GraphiteMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link GraphiteMetricsExportAutoConfiguration}. * Tests for {@link GraphiteMetricsExportAutoConfiguration}.
@ -106,27 +100,14 @@ public class GraphiteMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
GraphiteMeterRegistry registry = spyOnDisposableBean( GraphiteMeterRegistry registry = context
GraphiteMeterRegistry.class, context); .getBean(GraphiteMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.influx; package org.springframework.boot.actuate.autoconfigure.metrics.export.influx;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.influx.InfluxConfig; import io.micrometer.influx.InfluxConfig;
import io.micrometer.influx.InfluxMeterRegistry; import io.micrometer.influx.InfluxMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link InfluxMetricsExportAutoConfiguration}. * Tests for {@link InfluxMetricsExportAutoConfiguration}.
@ -89,27 +83,14 @@ public class InfluxMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
InfluxMeterRegistry registry = spyOnDisposableBean( InfluxMeterRegistry registry = context
InfluxMeterRegistry.class, context); .getBean(InfluxMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.jmx; package org.springframework.boot.actuate.autoconfigure.metrics.export.jmx;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.jmx.JmxConfig; import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry; import io.micrometer.jmx.JmxMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link JmxMetricsExportAutoConfiguration}. * Tests for {@link JmxMetricsExportAutoConfiguration}.
@ -89,27 +83,13 @@ public class JmxMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
JmxMeterRegistry registry = spyOnDisposableBean( JmxMeterRegistry registry = context.getBean(JmxMeterRegistry.class);
JmxMeterRegistry.class, context); assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic; package org.springframework.boot.actuate.autoconfigure.metrics.export.newrelic;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.newrelic.NewRelicConfig; import io.micrometer.newrelic.NewRelicConfig;
import io.micrometer.newrelic.NewRelicMeterRegistry; import io.micrometer.newrelic.NewRelicMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; 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", .withPropertyValues("management.metrics.export.newrelic.api-key=abcde",
"management.metrics.export.newrelic.account-id=abcde") "management.metrics.export.newrelic.account-id=abcde")
.withUserConfiguration(BaseConfiguration.class).run((context) -> { .withUserConfiguration(BaseConfiguration.class).run((context) -> {
NewRelicMeterRegistry registry = spyOnDisposableBean( NewRelicMeterRegistry registry = context
NewRelicMeterRegistry.class, context); .getBean(NewRelicMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx; package org.springframework.boot.actuate.autoconfigure.metrics.export.signalfx;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.signalfx.SignalFxConfig; import io.micrometer.signalfx.SignalFxConfig;
import io.micrometer.signalfx.SignalFxMeterRegistry; import io.micrometer.signalfx.SignalFxMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link SignalFxMetricsExportAutoConfiguration}. * Tests for {@link SignalFxMetricsExportAutoConfiguration}.
@ -106,27 +100,14 @@ public class SignalFxMetricsExportAutoConfigurationTests {
.withPropertyValues( .withPropertyValues(
"management.metrics.export.signalfx.access-token=abcde") "management.metrics.export.signalfx.access-token=abcde")
.withUserConfiguration(BaseConfiguration.class).run((context) -> { .withUserConfiguration(BaseConfiguration.class).run((context) -> {
SignalFxMeterRegistry registry = spyOnDisposableBean( SignalFxMeterRegistry registry = context
SignalFxMeterRegistry.class, context); .getBean(SignalFxMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,8 +16,6 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.statsd; package org.springframework.boot.actuate.autoconfigure.metrics.export.statsd;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.statsd.StatsdConfig; import io.micrometer.statsd.StatsdConfig;
import io.micrometer.statsd.StatsdMeterRegistry; import io.micrometer.statsd.StatsdMeterRegistry;
@ -25,16 +23,12 @@ import io.micrometer.statsd.StatsdMetrics;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link StatsdMetricsExportAutoConfiguration}. * Tests for {@link StatsdMetricsExportAutoConfiguration}.
@ -91,27 +85,14 @@ public class StatsdMetricsExportAutoConfigurationTests {
public void stopsMeterRegistryWhenContextIsClosed() { public void stopsMeterRegistryWhenContextIsClosed() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.run((context) -> { .run((context) -> {
StatsdMeterRegistry registry = spyOnDisposableBean( StatsdMeterRegistry registry = context
StatsdMeterRegistry.class, context); .getBean(StatsdMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

@ -16,24 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront; package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront;
import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.wavefront.WavefrontConfig; import io.micrometer.wavefront.WavefrontConfig;
import io.micrometer.wavefront.WavefrontMeterRegistry; import io.micrometer.wavefront.WavefrontMeterRegistry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link WavefrontMetricsExportAutoConfiguration}. * Tests for {@link WavefrontMetricsExportAutoConfiguration}.
@ -90,27 +84,14 @@ public class WavefrontMetricsExportAutoConfigurationTests {
this.contextRunner.withUserConfiguration(BaseConfiguration.class) this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.wavefront.api-token=abcde") .withPropertyValues("management.metrics.export.wavefront.api-token=abcde")
.run((context) -> { .run((context) -> {
WavefrontMeterRegistry registry = spyOnDisposableBean( WavefrontMeterRegistry registry = context
WavefrontMeterRegistry.class, context); .getBean(WavefrontMeterRegistry.class);
assertThat(registry.isClosed()).isFalse();
context.close(); 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 @Configuration
static class BaseConfiguration { static class BaseConfiguration {

Loading…
Cancel
Save