From ab43bf464308860d7ffb98feed707e7802d5f62a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 25 Jul 2017 08:46:30 +0200 Subject: [PATCH] Polish --- .../InfluxDbAutoConfigurationTests.java | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index 38d2b18d72..fa5cdfc538 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -20,13 +20,13 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; -import org.junit.After; import org.junit.Test; import retrofit2.Retrofit; import org.springframework.beans.DirectFieldAccessor; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,55 +41,57 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class InfluxDbAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void tearDown() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextTester context = new ApplicationContextTester() + .withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class)); @Test public void influxDbRequiresUrl() { - load(); - assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty(); + this.context.run((loaded) -> + assertThat(loaded.getBeansOfType(InfluxDB.class)).isEmpty()); } @Test public void influxDbCanBeCustomized() { - load("spring.influx.url=http://localhost", "spring.influx.password:password", - "spring.influx.user:user"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + this.context.withPropertyValues("spring.influx.url=http://localhost", + "spring.influx.password:password", "spring.influx.user:user") + .run((loaded -> + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1))); } @Test public void influxDbCanBeCreatedWithoutCredentials() { - load("spring.influx.url=http://localhost"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(10_000); + this.context.withPropertyValues("spring.influx.url=http://localhost") + .run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(10_000); + }); } @Test public void influxDbWithoutCredentialsAndOkHttpClientBuilder() { - load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(30_000); + this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost").run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(30_000); + }); } @Test public void influxDbWithOkHttpClientBuilder() { - load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost", - "spring.influx.password:password", "spring.influx.user:user"); - assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(); - assertThat(readTimeout).isEqualTo(30_000); + this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost", + "spring.influx.password:password", "spring.influx.user:user") + .run((loaded) -> { + assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(loaded); + assertThat(readTimeout).isEqualTo(30_000); + }); } - private int getReadTimeoutProperty() { - InfluxDB influxDB = this.context.getBean(InfluxDB.class); + private int getReadTimeoutProperty(AssertableApplicationContext loaded) { + InfluxDB influxDB = loaded.getBean(InfluxDB.class); Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB) .getPropertyValue("retrofit"); OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit) @@ -97,21 +99,6 @@ public class InfluxDbAutoConfigurationTests { return callFactory.readTimeoutMillis(); } - private void load(Class clazz, String... environment) { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(ctx); - ctx.register(InfluxDbAutoConfiguration.class); - if (clazz != null) { - ctx.register(clazz); - } - ctx.refresh(); - this.context = ctx; - } - - private void load(String... environment) { - load(null, environment); - } - @Configuration static class CustomOkHttpClientBuilderConfig {