diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java index ea46bc95d20..99ed01b22e5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java index 294129b9142..d0e5d16f3b3 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -27,7 +27,9 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; 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.ContextConsumer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -47,53 +49,43 @@ class JvmMetricsAutoConfigurationTests { @Test void autoConfiguresJvmMetrics() { - this.contextRunner.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) - .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class)); + this.contextRunner.run(assertMetricsBeans()); } @Test void allowsCustomJvmGcMetricsToBeUsed() { this.contextRunner.withUserConfiguration(CustomJvmGcMetricsConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasBean("customJvmGcMetrics") - .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class)); + .run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmGcMetrics"))); } @Test void allowsCustomJvmMemoryMetricsToBeUsed() { this.contextRunner.withUserConfiguration(CustomJvmMemoryMetricsConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) - .hasSingleBean(JvmMemoryMetrics.class).hasBean("customJvmMemoryMetrics") - .hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class) - .hasSingleBean(DiskSpaceMetrics.class)); + .run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmMemoryMetrics"))); } @Test void allowsCustomJvmThreadMetricsToBeUsed() { this.contextRunner.withUserConfiguration(CustomJvmThreadMetricsConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) - .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasBean("customJvmThreadMetrics").hasSingleBean(ClassLoaderMetrics.class) - .hasSingleBean(DiskSpaceMetrics.class)); + .run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmThreadMetrics"))); } @Test void allowsCustomClassLoaderMetricsToBeUsed() { - this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) - .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasBean("customClassLoaderMetrics") - .hasSingleBean(DiskSpaceMetrics.class)); + this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class).run( + assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customClassLoaderMetrics"))); } @Test void allowsCustomDiskSpaceMetricsToBeUsed() { this.contextRunner.withUserConfiguration(CustomDiskSpaceMetricsConfiguration.class) - .run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class) - .hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class) - .hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class) - .hasBean("customDiskSpaceMetrics")); + .run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customDiskSpaceMetrics"))); + } + + private ContextConsumer assertMetricsBeans() { + return (context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasSingleBean(JvmMemoryMetrics.class) + .hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class) + .hasSingleBean(DiskSpaceMetrics.class); } @Configuration(proxyBeanMethods = false)