From f0e93f0e1da16c0b97713959d21a7edccc1201db Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 27 Jan 2020 13:52:38 +0100 Subject: [PATCH] Migrate Couchbase tests to ApplicationContextRunner --- .../CouchbaseDataAutoConfigurationTests.java | 109 ++++++++---------- ...iveRepositoriesAutoConfigurationTests.java | 23 +--- ...aseReactiveDataAutoConfigurationTests.java | 68 +++++------ ...iveRepositoriesAutoConfigurationTests.java | 59 ++++------ ...aseRepositoriesAutoConfigurationTests.java | 52 +++------ 5 files changed, 120 insertions(+), 191 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java index 19305d0f17..03346cfc95 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseDataAutoConfigurationTests.java @@ -19,18 +19,16 @@ package org.springframework.boot.autoconfigure.data.couchbase; import java.util.Collections; import java.util.Set; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties; import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -56,101 +54,94 @@ import static org.assertj.core.api.Assertions.assertThat; */ class CouchbaseDataAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @AfterEach - void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class, + CouchbaseAutoConfiguration.class, CouchbaseDataAutoConfiguration.class)); @Test void disabledIfCouchbaseIsNotConfigured() { - load(null); - assertThat(this.context.getBeansOfType(IndexManager.class)).isEmpty(); + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(IndexManager.class)); } @Test void customConfiguration() { - load(CustomCouchbaseConfiguration.class); - CouchbaseTemplate couchbaseTemplate = this.context.getBean(CouchbaseTemplate.class); - assertThat(couchbaseTemplate.getDefaultConsistency()).isEqualTo(Consistency.STRONGLY_CONSISTENT); + this.contextRunner.withUserConfiguration(CustomCouchbaseConfiguration.class).run((context) -> { + CouchbaseTemplate couchbaseTemplate = context.getBean(CouchbaseTemplate.class); + assertThat(couchbaseTemplate.getDefaultConsistency()).isEqualTo(Consistency.STRONGLY_CONSISTENT); + }); } @Test void validatorIsPresent() { - load(CouchbaseTestConfigurer.class); - assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).hasSize(1); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .run((context) -> assertThat(context).hasSingleBean(ValidatingCouchbaseEventListener.class)); } @Test void autoIndexIsDisabledByDefault() { - load(CouchbaseTestConfigurer.class); - IndexManager indexManager = this.context.getBean(IndexManager.class); - assertThat(indexManager.isIgnoreViews()).isTrue(); - assertThat(indexManager.isIgnoreN1qlPrimary()).isTrue(); - assertThat(indexManager.isIgnoreN1qlSecondary()).isTrue(); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class).run((context) -> { + IndexManager indexManager = context.getBean(IndexManager.class); + assertThat(indexManager.isIgnoreViews()).isTrue(); + assertThat(indexManager.isIgnoreN1qlPrimary()).isTrue(); + assertThat(indexManager.isIgnoreN1qlSecondary()).isTrue(); + }); } @Test void enableAutoIndex() { - load(CouchbaseTestConfigurer.class, "spring.data.couchbase.auto-index=true"); - IndexManager indexManager = this.context.getBean(IndexManager.class); - assertThat(indexManager.isIgnoreViews()).isFalse(); - assertThat(indexManager.isIgnoreN1qlPrimary()).isFalse(); - assertThat(indexManager.isIgnoreN1qlSecondary()).isFalse(); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .withPropertyValues("spring.data.couchbase.auto-index=true").run((context) -> { + IndexManager indexManager = context.getBean(IndexManager.class); + assertThat(indexManager.isIgnoreViews()).isFalse(); + assertThat(indexManager.isIgnoreN1qlPrimary()).isFalse(); + assertThat(indexManager.isIgnoreN1qlSecondary()).isFalse(); + }); } @Test void changeConsistency() { - load(CouchbaseTestConfigurer.class, "spring.data.couchbase.consistency=eventually-consistent"); - SpringBootCouchbaseDataConfiguration configuration = this.context - .getBean(SpringBootCouchbaseDataConfiguration.class); - assertThat(configuration.getDefaultConsistency()).isEqualTo(Consistency.EVENTUALLY_CONSISTENT); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .withPropertyValues("spring.data.couchbase.consistency=eventually-consistent").run((context) -> { + SpringBootCouchbaseDataConfiguration configuration = context + .getBean(SpringBootCouchbaseDataConfiguration.class); + assertThat(configuration.getDefaultConsistency()).isEqualTo(Consistency.EVENTUALLY_CONSISTENT); + }); } @Test @SuppressWarnings("unchecked") void entityScanShouldSetInitialEntitySet() { - load(EntityScanConfig.class); - CouchbaseMappingContext mappingContext = this.context.getBean(CouchbaseMappingContext.class); - Set> initialEntitySet = (Set>) ReflectionTestUtils.getField(mappingContext, - "initialEntitySet"); - assertThat(initialEntitySet).containsOnly(City.class); + this.contextRunner.withUserConfiguration(EntityScanConfig.class).run((context) -> { + CouchbaseMappingContext mappingContext = context.getBean(CouchbaseMappingContext.class); + Set> initialEntitySet = (Set>) ReflectionTestUtils.getField(mappingContext, + "initialEntitySet"); + assertThat(initialEntitySet).containsOnly(City.class); + }); } @Test void typeKeyDefault() { - load(CouchbaseTestConfigurer.class); - assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()) - .isEqualTo(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .run((context) -> assertThat(context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()) + .isEqualTo(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY)); } @Test void typeKeyCanBeCustomized() { - load(CouchbaseTestConfigurer.class, "spring.data.couchbase.type-key=_custom"); - assertThat(this.context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()).isEqualTo("_custom"); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .withPropertyValues("spring.data.couchbase.type-key=_custom") + .run((context) -> assertThat(context.getBean(AbstractCouchbaseDataConfiguration.class).typeKey()) + .isEqualTo("_custom")); } @Test void customConversions() { - load(CustomConversionsConfig.class); - CouchbaseTemplate template = this.context.getBean(CouchbaseTemplate.class); - assertThat(template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class)) - .isTrue(); - } - - private void load(Class config, String... environment) { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(context); - if (config != null) { - context.register(config); - } - context.register(PropertyPlaceholderAutoConfiguration.class, ValidationAutoConfiguration.class, - CouchbaseAutoConfiguration.class, CouchbaseDataAutoConfiguration.class); - context.refresh(); - this.context = context; + this.contextRunner.withUserConfiguration(CustomConversionsConfig.class).run((context) -> { + CouchbaseTemplate template = context.getBean(CouchbaseTemplate.class); + assertThat( + template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class)) + .isTrue(); + }); } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java index 64edbbbb10..b08a2541b3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java @@ -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. @@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.data.couchbase; import java.util.ArrayList; import java.util.List; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; @@ -27,8 +26,7 @@ import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfigurati import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository; import org.springframework.boot.autoconfigure.data.couchbase.city.ReactiveCityRepository; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; @@ -47,21 +45,12 @@ import static org.assertj.core.api.Assertions.assertThat; */ class CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @AfterEach - void close() { - this.context.close(); - } - @Test void shouldCreateInstancesForReactiveAndImperativeRepositories() { - this.context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of("spring.datasource.initialization-mode:never").applyTo(this.context); - this.context.register(ImperativeAndReactiveConfiguration.class, BaseConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBean(CityRepository.class)).isNotNull(); - assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull(); + new ApplicationContextRunner() + .withUserConfiguration(ImperativeAndReactiveConfiguration.class, BaseConfiguration.class) + .withPropertyValues("spring.datasource.initialization-mode:never").run((context) -> assertThat(context) + .hasSingleBean(CityRepository.class).hasSingleBean(ReactiveCityRepository.class)); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveDataAutoConfigurationTests.java index 6a95904f29..df57cb1d05 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveDataAutoConfigurationTests.java @@ -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. @@ -19,18 +19,16 @@ package org.springframework.boot.autoconfigure.data.couchbase; import java.util.Collections; import java.util.Set; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties; import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -52,66 +50,52 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link CouchbaseReactiveDataAutoConfiguration}. * * @author Alex Derkach + * @author Stephane Nicoll */ class CouchbaseReactiveDataAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @AfterEach - void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration( + AutoConfigurations.of(ValidationAutoConfiguration.class, CouchbaseAutoConfiguration.class, + CouchbaseDataAutoConfiguration.class, CouchbaseReactiveDataAutoConfiguration.class)); @Test void disabledIfCouchbaseIsNotConfigured() { - load(null); - assertThat(this.context.getBeansOfType(IndexManager.class)).isEmpty(); + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(IndexManager.class)); } @Test void customConfiguration() { - load(CustomCouchbaseConfiguration.class); - RxJavaCouchbaseTemplate rxJavaCouchbaseTemplate = this.context.getBean(RxJavaCouchbaseTemplate.class); - assertThat(rxJavaCouchbaseTemplate.getDefaultConsistency()).isEqualTo(Consistency.STRONGLY_CONSISTENT); + this.contextRunner.withUserConfiguration(CustomCouchbaseConfiguration.class).run((context) -> { + RxJavaCouchbaseTemplate rxJavaCouchbaseTemplate = context.getBean(RxJavaCouchbaseTemplate.class); + assertThat(rxJavaCouchbaseTemplate.getDefaultConsistency()).isEqualTo(Consistency.STRONGLY_CONSISTENT); + }); } @Test void validatorIsPresent() { - load(CouchbaseTestConfigurer.class); - assertThat(this.context.getBeansOfType(ValidatingCouchbaseEventListener.class)).hasSize(1); + this.contextRunner.withUserConfiguration(CouchbaseTestConfigurer.class) + .run((context) -> assertThat(context).hasSingleBean(ValidatingCouchbaseEventListener.class)); } @Test @SuppressWarnings("unchecked") void entityScanShouldSetInitialEntitySet() { - load(EntityScanConfig.class); - CouchbaseMappingContext mappingContext = this.context.getBean(CouchbaseMappingContext.class); - Set> initialEntitySet = (Set>) ReflectionTestUtils.getField(mappingContext, - "initialEntitySet"); - assertThat(initialEntitySet).containsOnly(City.class); + this.contextRunner.withUserConfiguration(EntityScanConfig.class).run((context) -> { + CouchbaseMappingContext mappingContext = context.getBean(CouchbaseMappingContext.class); + Set> initialEntitySet = (Set>) ReflectionTestUtils.getField(mappingContext, + "initialEntitySet"); + assertThat(initialEntitySet).containsOnly(City.class); + }); } @Test void customConversions() { - load(CustomConversionsConfig.class); - RxJavaCouchbaseTemplate template = this.context.getBean(RxJavaCouchbaseTemplate.class); - assertThat(template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class)) - .isTrue(); - } - - private void load(Class config, String... environment) { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(context); - if (config != null) { - context.register(config); - } - context.register(PropertyPlaceholderAutoConfiguration.class, ValidationAutoConfiguration.class, - CouchbaseAutoConfiguration.class, CouchbaseDataAutoConfiguration.class, - CouchbaseReactiveDataAutoConfiguration.class); - context.refresh(); - this.context = context; + this.contextRunner.withUserConfiguration(CustomConversionsConfig.class).run((context) -> { + RxJavaCouchbaseTemplate template = context.getBean(RxJavaCouchbaseTemplate.class); + assertThat( + template.getConverter().getConversionService().canConvert(CouchbaseProperties.class, Boolean.class)) + .isTrue(); + }); } @Configuration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveRepositoriesAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveRepositoriesAutoConfigurationTests.java index 913679474c..7ddf6692a7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveRepositoriesAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveRepositoriesAutoConfigurationTests.java @@ -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. @@ -16,11 +16,10 @@ package org.springframework.boot.autoconfigure.data.couchbase; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; import org.springframework.boot.autoconfigure.data.alt.couchbase.CityCouchbaseRepository; @@ -28,8 +27,7 @@ import org.springframework.boot.autoconfigure.data.alt.couchbase.ReactiveCityCou import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.data.couchbase.city.ReactiveCityRepository; import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; @@ -40,65 +38,50 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link CouchbaseReactiveRepositoriesAutoConfiguration}. * * @author Alex Derkach + * @author Stephane Nicoll */ class CouchbaseReactiveRepositoriesAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @AfterEach - void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration( + AutoConfigurations.of(CouchbaseAutoConfiguration.class, CouchbaseDataAutoConfiguration.class, + CouchbaseRepositoriesAutoConfiguration.class, CouchbaseReactiveDataAutoConfiguration.class, + CouchbaseReactiveRepositoriesAutoConfiguration.class)); @Test void couchbaseNotAvailable() { - load(null); - assertThat(this.context.getBeansOfType(ReactiveCityRepository.class)).hasSize(0); + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ReactiveCityRepository.class)); } @Test void defaultRepository() { - load(DefaultConfiguration.class); - assertThat(this.context.getBeansOfType(ReactiveCityRepository.class)).hasSize(1); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .run((context) -> assertThat(context).hasSingleBean(ReactiveCityRepository.class)); } @Test void imperativeRepositories() { - load(DefaultConfiguration.class, "spring.data.couchbase.repositories.type=imperative"); - assertThat(this.context.getBeansOfType(ReactiveCityRepository.class)).hasSize(0); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .withPropertyValues("spring.data.couchbase.repositories.type=imperative") + .run((context) -> assertThat(context).doesNotHaveBean(ReactiveCityRepository.class)); } @Test void disabledRepositories() { - load(DefaultConfiguration.class, "spring.data.couchbase.repositories.type=none"); - assertThat(this.context.getBeansOfType(ReactiveCityRepository.class)).hasSize(0); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .withPropertyValues("spring.data.couchbase.repositories.type=none") + .run((context) -> assertThat(context).doesNotHaveBean(ReactiveCityRepository.class)); } @Test void noRepositoryAvailable() { - load(NoRepositoryConfiguration.class); - assertThat(this.context.getBeansOfType(ReactiveCityRepository.class)).hasSize(0); + this.contextRunner.withUserConfiguration(NoRepositoryConfiguration.class) + .run((context) -> assertThat(context).doesNotHaveBean(ReactiveCityRepository.class)); } @Test void doesNotTriggerDefaultRepositoryDetectionIfCustomized() { - load(CustomizedConfiguration.class); - assertThat(this.context.getBeansOfType(ReactiveCityCouchbaseRepository.class)).isEmpty(); - } - - private void load(Class config, String... environment) { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(context); - if (config != null) { - context.register(config); - } - context.register(PropertyPlaceholderAutoConfiguration.class, CouchbaseAutoConfiguration.class, - CouchbaseDataAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class, - CouchbaseReactiveDataAutoConfiguration.class, CouchbaseReactiveRepositoriesAutoConfiguration.class); - context.refresh(); - this.context = context; + this.contextRunner.withUserConfiguration(CustomizedConfiguration.class) + .run((context) -> assertThat(context).doesNotHaveBean(ReactiveCityCouchbaseRepository.class)); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java index 1659d1049d..8720aab8ad 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseRepositoriesAutoConfigurationTests.java @@ -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. @@ -16,18 +16,16 @@ package org.springframework.boot.autoconfigure.data.couchbase; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer; import org.springframework.boot.autoconfigure.data.couchbase.city.City; import org.springframework.boot.autoconfigure.data.couchbase.city.CityRepository; import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -41,55 +39,39 @@ import static org.assertj.core.api.Assertions.assertThat; */ class CouchbaseRepositoriesAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @AfterEach - void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(CouchbaseAutoConfiguration.class, + CouchbaseDataAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class)); @Test void couchbaseNotAvailable() { - load(null); - assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class)); } @Test void defaultRepository() { - load(DefaultConfiguration.class); - assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(1); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .run((context) -> assertThat(context).hasSingleBean(CityRepository.class)); } @Test void reactiveRepositories() { - load(DefaultConfiguration.class, "spring.data.couchbase.repositories.type=reactive"); - assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .withPropertyValues("spring.data.couchbase.repositories.type=reactive") + .run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class)); } @Test void disabledRepositories() { - load(DefaultConfiguration.class, "spring.data.couchbase.repositories.type=none"); - assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); + this.contextRunner.withUserConfiguration(DefaultConfiguration.class) + .withPropertyValues("spring.data.couchbase.repositories.type=none") + .run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class)); } @Test void noRepositoryAvailable() { - load(NoRepositoryConfiguration.class); - assertThat(this.context.getBeansOfType(CityRepository.class)).hasSize(0); - } - - private void load(Class config, String... environment) { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).applyTo(context); - if (config != null) { - context.register(config); - } - context.register(PropertyPlaceholderAutoConfiguration.class, CouchbaseAutoConfiguration.class, - CouchbaseDataAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class); - context.refresh(); - this.context = context; + this.contextRunner.withUserConfiguration(NoRepositoryConfiguration.class) + .run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class)); } @Configuration(proxyBeanMethods = false)