Migrate tests to use withBean

See gh-16011
pull/16513/head
Stephane Nicoll 6 years ago
parent a780875390
commit a828072132

@ -22,8 +22,6 @@ import org.springframework.boot.actuate.cache.CachesEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -42,7 +40,7 @@ public class CachesEndpointAutoConfigurationTests {
@Test @Test
public void runShouldHaveEndpointBean() { public void runShouldHaveEndpointBean() {
this.contextRunner.withUserConfiguration(CacheConfiguration.class) this.contextRunner.withBean(CacheManager.class, () -> mock(CacheManager.class))
.withPropertyValues("management.endpoints.web.exposure.include=caches") .withPropertyValues("management.endpoints.web.exposure.include=caches")
.run((context) -> assertThat(context) .run((context) -> assertThat(context)
.hasSingleBean(CachesEndpoint.class)); .hasSingleBean(CachesEndpoint.class));
@ -58,27 +56,18 @@ public class CachesEndpointAutoConfigurationTests {
@Test @Test
public void runWhenNotExposedShouldNotHaveEndpointBean() { public void runWhenNotExposedShouldNotHaveEndpointBean() {
this.contextRunner.withUserConfiguration(CacheConfiguration.class).run( this.contextRunner.withBean(CacheManager.class, () -> mock(CacheManager.class))
(context) -> assertThat(context).doesNotHaveBean(CachesEndpoint.class)); .run((context) -> assertThat(context)
.doesNotHaveBean(CachesEndpoint.class));
} }
@Test @Test
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() { public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() {
this.contextRunner.withPropertyValues("management.endpoint.caches.enabled:false") this.contextRunner.withPropertyValues("management.endpoint.caches.enabled:false")
.withPropertyValues("management.endpoints.web.exposure.include=*") .withPropertyValues("management.endpoints.web.exposure.include=*")
.withUserConfiguration(CacheConfiguration.class) .withBean(CacheManager.class, () -> mock(CacheManager.class))
.run((context) -> assertThat(context) .run((context) -> assertThat(context)
.doesNotHaveBean(CachesEndpoint.class)); .doesNotHaveBean(CachesEndpoint.class));
} }
@Configuration(proxyBeanMethods = false)
static class CacheConfiguration {
@Bean
public CacheManager cacheManager() {
return mock(CacheManager.class);
}
}
} }

@ -24,8 +24,6 @@ import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicat
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations; import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -40,7 +38,8 @@ import static org.mockito.Mockito.mock;
public class CassandraReactiveHealthIndicatorAutoConfigurationTests { public class CassandraReactiveHealthIndicatorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(CassandraMockConfiguration.class) .withBean(ReactiveCassandraOperations.class,
() -> mock(ReactiveCassandraOperations.class))
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
CassandraReactiveHealthIndicatorAutoConfiguration.class, CassandraReactiveHealthIndicatorAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class)); HealthIndicatorAutoConfiguration.class));
@ -61,14 +60,4 @@ public class CassandraReactiveHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
protected static class CassandraMockConfiguration {
@Bean
public ReactiveCassandraOperations cassandraOperations() {
return mock(ReactiveCassandraOperations.class);
}
}
} }

@ -242,7 +242,7 @@ public class ReactiveCloudFoundryActuatorAutoConfigurationTests {
@Test @Test
public void allEndpointsAvailableUnderCloudFoundryWithoutEnablingWebIncludes() { public void allEndpointsAvailableUnderCloudFoundryWithoutEnablingWebIncludes() {
this.contextRunner.withUserConfiguration(TestConfiguration.class) this.contextRunner.withBean(TestEndpoint.class, TestEndpoint::new)
.withPropertyValues("VCAP_APPLICATION:---", .withPropertyValues("VCAP_APPLICATION:---",
"vcap.application.application_id:my-app-id", "vcap.application.application_id:my-app-id",
"vcap.application.cf_api:https://my-cloud-controller.com") "vcap.application.cf_api:https://my-cloud-controller.com")
@ -260,7 +260,7 @@ public class ReactiveCloudFoundryActuatorAutoConfigurationTests {
@Test @Test
public void endpointPathCustomizationIsNotApplied() { public void endpointPathCustomizationIsNotApplied() {
this.contextRunner.withUserConfiguration(TestConfiguration.class) this.contextRunner.withBean(TestEndpoint.class, TestEndpoint::new)
.withPropertyValues("VCAP_APPLICATION:---", .withPropertyValues("VCAP_APPLICATION:---",
"vcap.application.application_id:my-app-id", "vcap.application.application_id:my-app-id",
"vcap.application.cf_api:https://my-cloud-controller.com") "vcap.application.cf_api:https://my-cloud-controller.com")
@ -380,16 +380,6 @@ public class ReactiveCloudFoundryActuatorAutoConfigurationTests {
+ requestPath + " from " + endpoint.getOperations()); + requestPath + " from " + endpoint.getOperations());
} }
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
public TestEndpoint testEndpoint() {
return new TestEndpoint();
}
}
@Endpoint(id = "test") @Endpoint(id = "test")
static class TestEndpoint { static class TestEndpoint {

@ -44,8 +44,6 @@ import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoC
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.config.BeanIds; import org.springframework.security.config.BeanIds;
@ -227,7 +225,7 @@ public class CloudFoundryActuatorAutoConfigurationTests {
@Test @Test
public void allEndpointsAvailableUnderCloudFoundryWithoutExposeAllOnWeb() { public void allEndpointsAvailableUnderCloudFoundryWithoutExposeAllOnWeb() {
this.contextRunner.withUserConfiguration(TestConfiguration.class) this.contextRunner.withBean(TestEndpoint.class, TestEndpoint::new)
.withPropertyValues("VCAP_APPLICATION:---", .withPropertyValues("VCAP_APPLICATION:---",
"vcap.application.application_id:my-app-id", "vcap.application.application_id:my-app-id",
"vcap.application.cf_api:https://my-cloud-controller.com") "vcap.application.cf_api:https://my-cloud-controller.com")
@ -250,7 +248,7 @@ public class CloudFoundryActuatorAutoConfigurationTests {
"vcap.application.application_id:my-app-id", "vcap.application.application_id:my-app-id",
"vcap.application.cf_api:https://my-cloud-controller.com", "vcap.application.cf_api:https://my-cloud-controller.com",
"management.endpoints.web.path-mapping.test=custom") "management.endpoints.web.path-mapping.test=custom")
.withUserConfiguration(TestConfiguration.class).run((context) -> { .withBean(TestEndpoint.class, TestEndpoint::new).run((context) -> {
CloudFoundryWebEndpointServletHandlerMapping handlerMapping = getHandlerMapping( CloudFoundryWebEndpointServletHandlerMapping handlerMapping = getHandlerMapping(
context); context);
Collection<ExposableWebEndpoint> endpoints = handlerMapping Collection<ExposableWebEndpoint> endpoints = handlerMapping
@ -309,16 +307,6 @@ public class CloudFoundryActuatorAutoConfigurationTests {
+ requestPath + " from " + endpoint.getOperations()); + requestPath + " from " + endpoint.getOperations());
} }
@Configuration(proxyBeanMethods = false)
static class TestConfiguration {
@Bean
public TestEndpoint testEndpoint() {
return new TestEndpoint();
}
}
@Endpoint(id = "test") @Endpoint(id = "test")
static class TestEndpoint { static class TestEndpoint {

@ -24,8 +24,6 @@ import org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicat
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -39,7 +37,7 @@ import static org.mockito.Mockito.mock;
public class CouchbaseHealthIndicatorAutoConfigurationTests { public class CouchbaseHealthIndicatorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(CouchbaseMockConfiguration.class).withConfiguration( .withBean(Cluster.class, () -> mock(Cluster.class)).withConfiguration(
AutoConfigurations.of(CouchbaseHealthIndicatorAutoConfiguration.class, AutoConfigurations.of(CouchbaseHealthIndicatorAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class)); HealthIndicatorAutoConfiguration.class));
@ -59,14 +57,4 @@ public class CouchbaseHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
protected static class CouchbaseMockConfiguration {
@Bean
public Cluster cluster() {
return mock(Cluster.class);
}
}
} }

@ -24,8 +24,6 @@ import org.springframework.boot.actuate.couchbase.CouchbaseReactiveHealthIndicat
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -38,7 +36,7 @@ import static org.mockito.Mockito.mock;
public class CouchbaseReactiveHealthIndicatorAutoConfigurationTests { public class CouchbaseReactiveHealthIndicatorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(CouchbaseMockConfiguration.class) .withBean(Cluster.class, () -> mock(Cluster.class))
.withConfiguration(AutoConfigurations.of( .withConfiguration(AutoConfigurations.of(
CouchbaseReactiveHealthIndicatorAutoConfiguration.class, CouchbaseReactiveHealthIndicatorAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class)); HealthIndicatorAutoConfiguration.class));
@ -59,14 +57,4 @@ public class CouchbaseReactiveHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
protected static class CouchbaseMockConfiguration {
@Bean
public Cluster couchbaseCluster() {
return mock(Cluster.class);
}
}
} }

@ -24,12 +24,8 @@ import org.springframework.boot.actuate.elasticsearch.ElasticsearchHealthIndicat
import org.springframework.boot.actuate.elasticsearch.ElasticsearchJestHealthIndicator; import org.springframework.boot.actuate.elasticsearch.ElasticsearchJestHealthIndicator;
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration; import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -61,7 +57,7 @@ public class ElasticsearchHealthIndicatorAutoConfigurationTests {
@Test @Test
public void runWhenUsingJestClientShouldCreateIndicator() { public void runWhenUsingJestClientShouldCreateIndicator() {
this.contextRunner.withUserConfiguration(JestClientConfiguration.class) this.contextRunner.withBean(JestClient.class, () -> mock(JestClient.class))
.withSystemProperties("es.set.netty.runtime.available.processors=false") .withSystemProperties("es.set.netty.runtime.available.processors=false")
.run((context) -> assertThat(context) .run((context) -> assertThat(context)
.hasSingleBean(ElasticsearchJestHealthIndicator.class) .hasSingleBean(ElasticsearchJestHealthIndicator.class)
@ -79,15 +75,4 @@ public class ElasticsearchHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(JestAutoConfiguration.class)
protected static class JestClientConfiguration {
@Bean
public JestClient jestClient() {
return mock(JestClient.class);
}
}
} }

@ -29,8 +29,6 @@ import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -45,7 +43,7 @@ public class JerseyWebEndpointManagementContextConfigurationTests {
private final WebApplicationContextRunner runner = new WebApplicationContextRunner() private final WebApplicationContextRunner runner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(WebEndpointAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(WebEndpointAutoConfiguration.class,
JerseyWebEndpointManagementContextConfiguration.class)) JerseyWebEndpointManagementContextConfiguration.class))
.withUserConfiguration(WebEndpointsSupplierConfig.class); .withBean(WebEndpointsSupplier.class, () -> Collections::emptyList);
@Test @Test
public void resourceConfigCustomizerForEndpointsIsAutoConfigured() { public void resourceConfigCustomizerForEndpointsIsAutoConfigured() {
@ -69,14 +67,4 @@ public class JerseyWebEndpointManagementContextConfigurationTests {
.doesNotHaveBean(JerseySameManagementContextConfiguration.class)); .doesNotHaveBean(JerseySameManagementContextConfiguration.class));
} }
@Configuration(proxyBeanMethods = false)
static class WebEndpointsSupplierConfig {
@Bean
public WebEndpointsSupplier webEndpointsSupplier() {
return Collections::emptyList;
}
}
} }

@ -22,8 +22,6 @@ import org.junit.Test;
import org.springframework.boot.actuate.flyway.FlywayEndpoint; import org.springframework.boot.actuate.flyway.FlywayEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -38,7 +36,7 @@ public class FlywayEndpointAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration( .withConfiguration(
AutoConfigurations.of(FlywayEndpointAutoConfiguration.class)) AutoConfigurations.of(FlywayEndpointAutoConfiguration.class))
.withUserConfiguration(FlywayConfiguration.class); .withBean(Flyway.class, () -> mock(Flyway.class));
@Test @Test
public void runShouldHaveEndpointBean() { public void runShouldHaveEndpointBean() {
@ -61,14 +59,4 @@ public class FlywayEndpointAutoConfigurationTests {
(context) -> assertThat(context).doesNotHaveBean(FlywayEndpoint.class)); (context) -> assertThat(context).doesNotHaveBean(FlywayEndpoint.class));
} }
@Configuration(proxyBeanMethods = false)
static class FlywayConfiguration {
@Bean
public Flyway flyway() {
return mock(Flyway.class);
}
}
} }

@ -26,8 +26,6 @@ import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Status; import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -52,7 +50,7 @@ public class HealthEndpointAutoConfigurationTests {
@Test @Test
public void healthEndpointShowDetailsDefault() { public void healthEndpointShowDetailsDefault() {
this.contextRunner this.contextRunner
.withUserConfiguration(ReactiveHealthIndicatorConfiguration.class) .withBean(ReactiveHealthIndicator.class, this::reactiveHealthIndicator)
.run((context) -> { .run((context) -> {
ReactiveHealthIndicator indicator = context.getBean( ReactiveHealthIndicator indicator = context.getBean(
"reactiveHealthIndicator", ReactiveHealthIndicator.class); "reactiveHealthIndicator", ReactiveHealthIndicator.class);
@ -68,7 +66,7 @@ public class HealthEndpointAutoConfigurationTests {
public void healthEndpointAdaptReactiveHealthIndicator() { public void healthEndpointAdaptReactiveHealthIndicator() {
this.contextRunner this.contextRunner
.withPropertyValues("management.endpoint.health.show-details=always") .withPropertyValues("management.endpoint.health.show-details=always")
.withUserConfiguration(ReactiveHealthIndicatorConfiguration.class) .withBean(ReactiveHealthIndicator.class, this::reactiveHealthIndicator)
.run((context) -> { .run((context) -> {
ReactiveHealthIndicator indicator = context.getBean( ReactiveHealthIndicator indicator = context.getBean(
"reactiveHealthIndicator", ReactiveHealthIndicator.class); "reactiveHealthIndicator", ReactiveHealthIndicator.class);
@ -84,8 +82,10 @@ public class HealthEndpointAutoConfigurationTests {
public void healthEndpointMergeRegularAndReactive() { public void healthEndpointMergeRegularAndReactive() {
this.contextRunner this.contextRunner
.withPropertyValues("management.endpoint.health.show-details=always") .withPropertyValues("management.endpoint.health.show-details=always")
.withUserConfiguration(HealthIndicatorConfiguration.class, .withBean("simpleHealthIndicator", HealthIndicator.class,
ReactiveHealthIndicatorConfiguration.class) this::simpleHealthIndicator)
.withBean("reactiveHealthIndicator", ReactiveHealthIndicator.class,
this::reactiveHealthIndicator)
.run((context) -> { .run((context) -> {
HealthIndicator indicator = context.getBean("simpleHealthIndicator", HealthIndicator indicator = context.getBean("simpleHealthIndicator",
HealthIndicator.class); HealthIndicator.class);
@ -102,28 +102,16 @@ public class HealthEndpointAutoConfigurationTests {
}); });
} }
@Configuration(proxyBeanMethods = false) private HealthIndicator simpleHealthIndicator() {
static class HealthIndicatorConfiguration { HealthIndicator mock = mock(HealthIndicator.class);
given(mock.health()).willReturn(Health.status(Status.UP).build());
@Bean return mock;
public HealthIndicator simpleHealthIndicator() {
HealthIndicator mock = mock(HealthIndicator.class);
given(mock.health()).willReturn(Health.status(Status.UP).build());
return mock;
}
} }
@Configuration(proxyBeanMethods = false) private ReactiveHealthIndicator reactiveHealthIndicator() {
static class ReactiveHealthIndicatorConfiguration { ReactiveHealthIndicator mock = mock(ReactiveHealthIndicator.class);
given(mock.health()).willReturn(Mono.just(Health.status(Status.UP).build()));
@Bean return mock;
public ReactiveHealthIndicator reactiveHealthIndicator() {
ReactiveHealthIndicator mock = mock(ReactiveHealthIndicator.class);
given(mock.health()).willReturn(Mono.just(Health.status(Status.UP).build()));
return mock;
}
} }
} }

@ -24,8 +24,6 @@ import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.influx.InfluxDbHealthIndicator; import org.springframework.boot.actuate.influx.InfluxDbHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
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.Configuration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -38,7 +36,7 @@ import static org.mockito.Mockito.mock;
public class InfluxDbHealthIndicatorAutoConfigurationTests { public class InfluxDbHealthIndicatorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(InfluxDbConfiguration.class).withConfiguration( .withBean(InfluxDB.class, () -> mock(InfluxDB.class)).withConfiguration(
AutoConfigurations.of(InfluxDbHealthIndicatorAutoConfiguration.class, AutoConfigurations.of(InfluxDbHealthIndicatorAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class)); HealthIndicatorAutoConfiguration.class));
@ -57,14 +55,4 @@ public class InfluxDbHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
static class InfluxDbConfiguration {
@Bean
public InfluxDB influxdb() {
return mock(InfluxDB.class);
}
}
} }

@ -22,10 +22,7 @@ import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAuto
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.ldap.LdapHealthIndicator; import org.springframework.boot.actuate.ldap.LdapHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
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.Configuration;
import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.LdapOperations;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -40,7 +37,8 @@ import static org.mockito.Mockito.mock;
public class LdapHealthIndicatorAutoConfigurationTests { public class LdapHealthIndicatorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(LdapConfiguration.class).withConfiguration( .withBean(LdapOperations.class, () -> mock(LdapOperations.class))
.withConfiguration(
AutoConfigurations.of(LdapHealthIndicatorAutoConfiguration.class, AutoConfigurations.of(LdapHealthIndicatorAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class)); HealthIndicatorAutoConfiguration.class));
@ -59,15 +57,4 @@ public class LdapHealthIndicatorAutoConfigurationTests {
.hasSingleBean(ApplicationHealthIndicator.class)); .hasSingleBean(ApplicationHealthIndicator.class));
} }
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(LdapHealthIndicatorAutoConfiguration.class)
protected static class LdapConfiguration {
@Bean
public LdapOperations ldapOperations() {
return mock(LdapOperations.class);
}
}
} }

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.autoconfigure.liquibase; package org.springframework.boot.actuate.autoconfigure.liquibase;
import liquibase.exception.LiquibaseException;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
import org.junit.Test; import org.junit.Test;
@ -45,14 +44,15 @@ public class LiquibaseEndpointAutoConfigurationTests {
public void runShouldHaveEndpointBean() { public void runShouldHaveEndpointBean() {
this.contextRunner this.contextRunner
.withPropertyValues("management.endpoints.web.exposure.include=liquibase") .withPropertyValues("management.endpoints.web.exposure.include=liquibase")
.withUserConfiguration(LiquibaseConfiguration.class) .withBean(SpringLiquibase.class, () -> mock(SpringLiquibase.class))
.run((context) -> assertThat(context) .run((context) -> assertThat(context)
.hasSingleBean(LiquibaseEndpoint.class)); .hasSingleBean(LiquibaseEndpoint.class));
} }
@Test @Test
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() { public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointBean() {
this.contextRunner.withUserConfiguration(LiquibaseConfiguration.class) this.contextRunner
.withBean(SpringLiquibase.class, () -> mock(SpringLiquibase.class))
.withPropertyValues("management.endpoint.liquibase.enabled:false") .withPropertyValues("management.endpoint.liquibase.enabled:false")
.run((context) -> assertThat(context) .run((context) -> assertThat(context)
.doesNotHaveBean(LiquibaseEndpoint.class)); .doesNotHaveBean(LiquibaseEndpoint.class));
@ -91,16 +91,6 @@ public class LiquibaseEndpointAutoConfigurationTests {
}); });
} }
@Configuration(proxyBeanMethods = false)
static class LiquibaseConfiguration {
@Bean
public SpringLiquibase liquibase() {
return mock(SpringLiquibase.class);
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class DataSourceClosingLiquibaseConfiguration { static class DataSourceClosingLiquibaseConfiguration {
@ -121,7 +111,7 @@ public class LiquibaseEndpointAutoConfigurationTests {
} }
@Override @Override
public void afterPropertiesSet() throws LiquibaseException { public void afterPropertiesSet() {
this.propertiesSet = true; this.propertiesSet = true;
} }

@ -21,14 +21,11 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.flyway.FlywayEndpoint.FlywayDescriptor; import org.springframework.boot.actuate.flyway.FlywayEndpoint.FlywayDescriptor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy; import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -41,57 +38,36 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class FlywayEndpointTests { public class FlywayEndpointTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withBean("endpoint", FlywayEndpoint.class, FlywayEndpoint::new);
@Test @Test
public void flywayReportIsProduced() { public void flywayReportIsProduced() {
new ApplicationContextRunner().withUserConfiguration(Config.class) this.contextRunner.run((context) -> {
.run((context) -> { Map<String, FlywayDescriptor> flywayBeans = context
Map<String, FlywayDescriptor> flywayBeans = context .getBean(FlywayEndpoint.class).flywayBeans().getContexts()
.getBean(FlywayEndpoint.class).flywayBeans().getContexts() .get(context.getId()).getFlywayBeans();
.get(context.getId()).getFlywayBeans(); assertThat(flywayBeans).hasSize(1);
assertThat(flywayBeans).hasSize(1); assertThat(flywayBeans.values().iterator().next().getMigrations()).hasSize(3);
assertThat(flywayBeans.values().iterator().next().getMigrations()) });
.hasSize(3);
});
} }
@Test @Test
@SuppressWarnings("deprecation")
public void whenFlywayHasBeenBaselinedFlywayReportIsProduced() { public void whenFlywayHasBeenBaselinedFlywayReportIsProduced() {
new ApplicationContextRunner() this.contextRunner.withBean(FlywayMigrationStrategy.class, () -> (flyway) -> {
.withUserConfiguration(BaselinedFlywayConfig.class, Config.class) flyway.setBaselineVersionAsString("2");
.run((context) -> { flyway.baseline();
Map<String, FlywayDescriptor> flywayBeans = context flyway.migrate();
.getBean(FlywayEndpoint.class).flywayBeans().getContexts() }).run((context) -> {
.get(context.getId()).getFlywayBeans(); Map<String, FlywayDescriptor> flywayBeans = context
assertThat(flywayBeans).hasSize(1); .getBean(FlywayEndpoint.class).flywayBeans().getContexts()
assertThat(flywayBeans.values().iterator().next().getMigrations()) .get(context.getId()).getFlywayBeans();
.hasSize(3); assertThat(flywayBeans).hasSize(1);
}); assertThat(flywayBeans.values().iterator().next().getMigrations()).hasSize(3);
} });
@Configuration(proxyBeanMethods = false)
@Import({ EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class })
public static class Config {
@Bean
public FlywayEndpoint endpoint(ApplicationContext context) {
return new FlywayEndpoint(context);
}
}
@Configuration(proxyBeanMethods = false)
public static class BaselinedFlywayConfig {
@SuppressWarnings("deprecation")
@Bean
public FlywayMigrationStrategy baseliningMigrationStrategy() {
return (flyway) -> {
flyway.setBaselineVersionAsString("2");
flyway.baseline();
flyway.migrate();
};
}
} }
} }

Loading…
Cancel
Save