From b0bf2dd7ab24a30bca09f8d6fad32c90e09cd629 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Sun, 14 Jul 2019 17:21:43 +0100 Subject: [PATCH] Migrate to ApplicationContextRunner Migrate a new tests to use the `ApplicationContextRunner`. See gh-17491 --- ...MissingBeanWithFilteredClasspathTests.java | 15 ++++--------- ...sAutoConfigurationWithoutJacksonTests.java | 18 +++++----------- ...hHibernateValidatorMissingElImplTests.java | 21 +++++++------------ ...utoConfigurationWithoutValidatorTests.java | 21 +++++++------------ 4 files changed, 25 insertions(+), 50 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanWithFilteredClasspathTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanWithFilteredClasspathTests.java index 7d97813a35..214318226c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanWithFilteredClasspathTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanWithFilteredClasspathTests.java @@ -16,14 +16,13 @@ package org.springframework.boot.autoconfigure.condition; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.cache.caffeine.CaffeineCacheManager; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -40,18 +39,12 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions("spring-context-support-*.jar") public class ConditionalOnMissingBeanWithFilteredClasspathTests { - private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - - @After - public void closeContext() { - this.context.close(); - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withUserConfiguration(OnBeanTypeConfiguration.class); @Test public void testNameOnMissingBeanTypeWithMissingImport() { - this.context.register(OnBeanTypeConfiguration.class); - this.context.refresh(); - assertThat(this.context.containsBean("foo")).isTrue(); + this.contextRunner.run((context) -> assertThat(context).hasBean("foo")); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationWithoutJacksonTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationWithoutJacksonTests.java index 6859d5a568..f8c26d0d09 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationWithoutJacksonTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfigurationWithoutJacksonTests.java @@ -16,13 +16,13 @@ package org.springframework.boot.autoconfigure.http; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -36,20 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions("jackson-*.jar") public class HttpMessageConvertersAutoConfigurationWithoutJacksonTests { - private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(HttpMessageConvertersAutoConfiguration.class)); @Test public void autoConfigurationWorksWithSpringHateoasButWithoutJackson() { - this.context.register(HttpMessageConvertersAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBeansOfType(HttpMessageConverters.class)).hasSize(1); + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(HttpMessageConverters.class)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests.java index 502f1c1777..9f1ca0dbac 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests.java @@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.validation; import javax.validation.Validator; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import static org.assertj.core.api.Assertions.assertThat; @@ -39,20 +39,15 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions({ "tomcat-embed-el-*.jar", "el-api-*.jar" }) public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests { - private AnnotationConfigApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class)); @Test public void missingElDependencyIsTolerated() { - this.context = new AnnotationConfigApplicationContext(ValidationAutoConfiguration.class); - assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1); - assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)).hasSize(1); + this.contextRunner.run((context) -> { + assertThat(context).hasSingleBean(Validator.class); + assertThat(context).hasSingleBean(MethodValidationPostProcessor.class); + }); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithoutValidatorTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithoutValidatorTests.java index b25bf16389..c5e2e600ce 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithoutValidatorTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationWithoutValidatorTests.java @@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.validation; import javax.validation.Validator; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import static org.assertj.core.api.Assertions.assertThat; @@ -38,20 +38,15 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions("hibernate-validator-*.jar") public class ValidationAutoConfigurationWithoutValidatorTests { - private AnnotationConfigApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class)); @Test public void validationIsDisabled() { - this.context = new AnnotationConfigApplicationContext(ValidationAutoConfiguration.class); - assertThat(this.context.getBeansOfType(Validator.class)).isEmpty(); - assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)).isEmpty(); + this.contextRunner.run((context) -> { + assertThat(context).doesNotHaveBean(Validator.class); + assertThat(context).doesNotHaveBean(MethodValidationPostProcessor.class); + }); } }