From c8c32cfa33657a8e51bc3e16a454fa50621cc97d Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 11 Jul 2018 14:53:26 -0700 Subject: [PATCH] Migrate tests to ApplicationContextRunner --- .../MongoDataAutoConfigurationTests.java | 153 ++++++++---------- ...ngoReactiveDataAutoConfigurationTests.java | 26 ++- 2 files changed, 79 insertions(+), 100 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java index 6fc852fd26..236c6ec72b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java @@ -21,17 +21,17 @@ import java.util.Arrays; import java.util.Set; import com.mongodb.MongoClient; -import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurationPackages; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.city.City; import org.springframework.boot.autoconfigure.data.mongo.country.Country; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; -import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -48,7 +48,6 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; /** * Tests for {@link MongoDataAutoConfiguration}. @@ -58,125 +57,111 @@ import static org.junit.Assert.fail; */ public class MongoDataAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + PropertyPlaceholderAutoConfiguration.class, + MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)); @Test public void templateExists() { - this.context = new AnnotationConfigApplicationContext( - PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, - MongoDataAutoConfiguration.class); - assertThat(this.context.getBeanNamesForType(MongoTemplate.class).length) - .isEqualTo(1); + this.contextRunner + .run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class)); } @Test public void gridFsTemplateExists() { - this.context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of("spring.data.mongodb.gridFsDatabase:grid") - .applyTo(this.context); - this.context.register(PropertyPlaceholderAutoConfiguration.class, - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); - this.context.refresh(); - assertThat(this.context.getBeanNamesForType(GridFsTemplate.class).length) - .isEqualTo(1); + this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid") + .run((context) -> assertThat(context) + .hasSingleBean(GridFsTemplate.class)); } @Test public void customConversions() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(CustomConversionsConfig.class); - this.context.register(PropertyPlaceholderAutoConfiguration.class, - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); - this.context.refresh(); - MongoTemplate template = this.context.getBean(MongoTemplate.class); - assertThat(template.getConverter().getConversionService() - .canConvert(MongoClient.class, Boolean.class)).isTrue(); + this.contextRunner.withUserConfiguration(CustomConversionsConfig.class) + .run((context) -> { + MongoTemplate template = context.getBean(MongoTemplate.class); + assertThat(template.getConverter().getConversionService() + .canConvert(MongoClient.class, Boolean.class)).isTrue(); + }); } @Test public void usesAutoConfigurationPackageToPickUpDocumentTypes() { - this.context = new AnnotationConfigApplicationContext(); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); String cityPackage = City.class.getPackage().getName(); - AutoConfigurationPackages.register(this.context, cityPackage); - this.context.register(MongoAutoConfiguration.class, - MongoDataAutoConfiguration.class); - this.context.refresh(); - assertDomainTypesDiscovered(this.context.getBean(MongoMappingContext.class), - City.class); + AutoConfigurationPackages.register(context, cityPackage); + context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); + try { + context.refresh(); + assertDomainTypesDiscovered(context.getBean(MongoMappingContext.class), + City.class); + } + finally { + context.close(); + } } @Test public void defaultFieldNamingStrategy() { - testFieldNamingStrategy(null, PropertyNameFieldNamingStrategy.class); + this.contextRunner.run((context) -> { + MongoMappingContext mappingContext = context + .getBean(MongoMappingContext.class); + FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils + .getField(mappingContext, "fieldNamingStrategy"); + assertThat(fieldNamingStrategy.getClass()) + .isEqualTo(PropertyNameFieldNamingStrategy.class); + }); } @Test public void customFieldNamingStrategy() { - testFieldNamingStrategy(CamelCaseAbbreviatingFieldNamingStrategy.class.getName(), - CamelCaseAbbreviatingFieldNamingStrategy.class); + this.contextRunner + .withPropertyValues("spring.data.mongodb.field-naming-strategy:" + + CamelCaseAbbreviatingFieldNamingStrategy.class.getName()) + .run((context) -> { + MongoMappingContext mappingContext = context + .getBean(MongoMappingContext.class); + FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils + .getField(mappingContext, "fieldNamingStrategy"); + assertThat(fieldNamingStrategy.getClass()) + .isEqualTo(CamelCaseAbbreviatingFieldNamingStrategy.class); + }); } @Test public void interfaceFieldNamingStrategy() { - try { - testFieldNamingStrategy(FieldNamingStrategy.class.getName(), null); - fail("Create FieldNamingStrategy interface should fail"); - } - // We seem to have an inconsistent exception, accept either - catch (BeanCreationException ex) { - // Expected - } + this.contextRunner + .withPropertyValues("spring.data.mongodb.field-naming-strategy:" + + FieldNamingStrategy.class.getName()) + .run((context) -> assertThat(context).getFailure() + .isInstanceOf(BeanCreationException.class)); } @Test @SuppressWarnings("unchecked") public void entityScanShouldSetInitialEntitySet() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(EntityScanConfig.class, - PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, - MongoDataAutoConfiguration.class); - this.context.refresh(); - MongoMappingContext mappingContext = this.context - .getBean(MongoMappingContext.class); - Set> initialEntitySet = (Set>) ReflectionTestUtils - .getField(mappingContext, "initialEntitySet"); - assertThat(initialEntitySet).containsOnly(City.class, Country.class); + this.contextRunner.withUserConfiguration(EntityScanConfig.class) + .run((context) -> { + MongoMappingContext mappingContext = context + .getBean(MongoMappingContext.class); + Set> initialEntitySet = (Set>) ReflectionTestUtils + .getField(mappingContext, "initialEntitySet"); + assertThat(initialEntitySet).containsOnly(City.class, Country.class); + }); + } @Test public void registersDefaultSimpleTypesWithMappingContext() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(MongoAutoConfiguration.class, - MongoDataAutoConfiguration.class); - this.context.refresh(); - MongoMappingContext context = this.context.getBean(MongoMappingContext.class); - BasicMongoPersistentEntity entity = context.getPersistentEntity(Sample.class); - MongoPersistentProperty dateProperty = entity.getPersistentProperty("date"); - assertThat(dateProperty.isEntity()).isFalse(); - } + this.contextRunner.run((context) -> { + MongoMappingContext mappingContext = context + .getBean(MongoMappingContext.class); + BasicMongoPersistentEntity entity = mappingContext + .getPersistentEntity(Sample.class); + MongoPersistentProperty dateProperty = entity.getPersistentProperty("date"); + assertThat(dateProperty.isEntity()).isFalse(); + }); - public void testFieldNamingStrategy(String strategy, - Class expectedType) { - this.context = new AnnotationConfigApplicationContext(); - if (strategy != null) { - TestPropertyValues.of("spring.data.mongodb.field-naming-strategy:" + strategy) - .applyTo(this.context); - } - this.context.register(PropertyPlaceholderAutoConfiguration.class, - MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); - this.context.refresh(); - MongoMappingContext mappingContext = this.context - .getBean(MongoMappingContext.class); - FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils - .getField(mappingContext, "fieldNamingStrategy"); - assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType); } @SuppressWarnings({ "unchecked", "rawtypes" }) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java index f207ab8531..50b583f01d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java @@ -16,13 +16,13 @@ package org.springframework.boot.autoconfigure.data.mongo; -import org.junit.After; import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import static org.assertj.core.api.Assertions.assertThat; @@ -34,23 +34,17 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class MongoReactiveDataAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + PropertyPlaceholderAutoConfiguration.class, + MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, + MongoReactiveAutoConfiguration.class, + MongoReactiveDataAutoConfiguration.class)); @Test public void templateExists() { - this.context = new AnnotationConfigApplicationContext( - PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, - MongoDataAutoConfiguration.class, MongoReactiveAutoConfiguration.class, - MongoReactiveDataAutoConfiguration.class); - assertThat(this.context.getBeanNamesForType(ReactiveMongoTemplate.class)) - .hasSize(1); + this.contextRunner.run((context) -> assertThat(context) + .hasSingleBean(ReactiveMongoTemplate.class)); } }