Polish "Disable metrics export in integration tests"
See gh-21658
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -28,30 +28,28 @@ import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizerFactory} that creates a customizer that globally disables
|
||||
* metrics exporters unless the {@link AutoConfigureMetrics} annotation is specified on
|
||||
* the test class.
|
||||
* {@link ContextCustomizerFactory} that globally disables metrics export unless
|
||||
* {@link AutoConfigureMetrics} is set on the test class.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
class ExcludeMetricExportersContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
|
||||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
boolean metricExportersEnabled = MergedAnnotations.from(testClass, SearchStrategy.TYPE_HIERARCHY)
|
||||
boolean disableMetricsExport = !MergedAnnotations.from(testClass, SearchStrategy.TYPE_HIERARCHY)
|
||||
.get(AutoConfigureMetrics.class).isPresent();
|
||||
return !metricExportersEnabled ? new ExcludeMetricExportersContextCustomizer() : null;
|
||||
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
|
||||
}
|
||||
|
||||
static class ExcludeMetricExportersContextCustomizer implements ContextCustomizer {
|
||||
static class DisableMetricExportContextCustomizer implements ContextCustomizer {
|
||||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
TestPropertyValues
|
||||
.of("management.metrics.export.enabled=false", "management.metrics.export.simple.enabled=true")
|
||||
.applyTo(context);
|
||||
TestPropertyValues.of("management.metrics.export.defaults.enabled=false",
|
||||
"management.metrics.export.simple.enabled=true").applyTo(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,6 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test auto-configuration support for actuator metrics.
|
||||
* Auto-configuration for handling metrics in tests.
|
||||
*/
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
@@ -185,8 +185,8 @@ org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExe
|
||||
# Spring Test ContextCustomizerFactories
|
||||
org.springframework.test.context.ContextCustomizerFactory=\
|
||||
org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,\
|
||||
org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory,\
|
||||
org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory,\
|
||||
org.springframework.boot.test.autoconfigure.metrics.ExcludeMetricExportersContextCustomizerFactory,\
|
||||
org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizerFactory,\
|
||||
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,12 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -33,13 +37,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@SpringBootTest
|
||||
class AutoConfigureMetricsMissingIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
@Test
|
||||
void customizerRunsAndOnlyEnablesSimpleMeterRegistryWhenNoAnnotationPresent(
|
||||
@Autowired ApplicationContext applicationContext) {
|
||||
assertThat(applicationContext.getBean(MeterRegistry.class)).isInstanceOf(SimpleMeterRegistry.class);
|
||||
assertThat(applicationContext.getBeansOfType(PrometheusMeterRegistry.class)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizerRunsAndSetsExclusionPropertiesWhenNoAnnotationPresent() {
|
||||
assertThat(this.environment.getProperty("management.metrics.export.enabled")).isEqualTo("false");
|
||||
assertThat(this.environment.getProperty("management.metrics.export.simple.enabled")).isEqualTo("true");
|
||||
void customizerRunsAndSetsExclusionPropertiesWhenNoAnnotationPresent(@Autowired Environment environment) {
|
||||
assertThat(environment.getProperty("management.metrics.export.defaults.enabled")).isEqualTo("false");
|
||||
assertThat(environment.getProperty("management.metrics.export.simple.enabled")).isEqualTo("true");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,12 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -34,13 +36,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@AutoConfigureMetrics
|
||||
class AutoConfigureMetricsPresentIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
@Test
|
||||
void customizerDoesNotDisableAvailableMeterRegistriesWhenAnnotationPresent(
|
||||
@Autowired ApplicationContext applicationContext) {
|
||||
assertThat(applicationContext.getBeansOfType(PrometheusMeterRegistry.class)).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizerDoesNotRunWhenAnnotationPresent() {
|
||||
assertThat(this.environment.containsProperty("management.metrics.export.enabled")).isFalse();
|
||||
assertThat(this.environment.containsProperty("management.metrics.export.simple.enabled")).isFalse();
|
||||
void customizerDoesNotSetExclusionPropertiesWhenAnnotationPresent(@Autowired Environment environment) {
|
||||
assertThat(environment.containsProperty("management.metrics.export.enabled")).isFalse();
|
||||
assertThat(environment.containsProperty("management.metrics.export.simple.enabled")).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,7 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.metrics;
|
||||
package org.springframework.boot.test.autoconfigure.actuate.metrics;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -26,21 +28,23 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AutoConfigureMetrics} and
|
||||
* {@link ExcludeMetricExportersContextCustomizerFactory} working together.
|
||||
* {@link MetricsExportContextCustomizerFactory} working together.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
class ExcludeMetricExportersContextCustomizerFactoryTests {
|
||||
class MetricsExportContextCustomizerFactoryTests {
|
||||
|
||||
private ExcludeMetricExportersContextCustomizerFactory factory = new ExcludeMetricExportersContextCustomizerFactory();
|
||||
private final MetricsExportContextCustomizerFactory factory = new MetricsExportContextCustomizerFactory();
|
||||
|
||||
@Test
|
||||
void getContextCustomizerWhenHasNoAnnotationShouldReturnCustomizer() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoAnnotation.class, null);
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoAnnotation.class,
|
||||
Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
ConfigurableApplicationContext context = new GenericApplicationContext();
|
||||
customizer.customizeContext(context, null);
|
||||
assertThat(context.getEnvironment().getProperty("management.metrics.export.enabled")).isEqualTo("false");
|
||||
assertThat(context.getEnvironment().getProperty("management.metrics.export.defaults.enabled"))
|
||||
.isEqualTo("false");
|
||||
assertThat(context.getEnvironment().getProperty("management.metrics.export.simple.enabled")).isEqualTo("true");
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.restdocs;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
|
||||
@@ -24,7 +25,7 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfi
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class })
|
||||
public class RestDocsTestApplication {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user