From 497457c397d9c45b127285c5cc3f2ba36aa5b890 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 26 Jul 2017 13:29:38 -0700 Subject: [PATCH 1/4] Rename ApplicationContextTester -> Runner Rename `ApplicationContextTester` and related classes to `ApplicationContextRunner` and refactor existing tests to use correctly named variables. See gh-9875 --- ...HealthIndicatorAutoConfigurationTests.java | 104 +++-- .../cache/CacheAutoConfigurationTests.java | 335 +++++++------- .../Neo4jDataAutoConfigurationTests.java | 61 +-- ...HazelcastAutoConfigurationClientTests.java | 35 +- ...HazelcastAutoConfigurationServerTests.java | 46 +- .../HazelcastAutoConfigurationTests.java | 8 +- .../InfluxDbAutoConfigurationTests.java | 41 +- .../DataSourceAutoConfigurationTests.java | 63 +-- .../jms/JmsAutoConfigurationTests.java | 174 ++++---- .../ActiveMQAutoConfigurationTests.java | 67 +-- .../ArtemisAutoConfigurationTests.java | 114 ++--- .../HttpHandlerAutoConfigurationTests.java | 21 +- .../servlet/WebMvcAutoConfigurationTests.java | 420 +++++++++--------- .../WebServicesAutoConfigurationTests.java | 37 +- .../TestDatabaseAutoConfigurationTests.java | 16 +- ...abaseAutoConfigurationNoEmbeddedTests.java | 18 +- ... => AbstractApplicationContextRunner.java} | 28 +- .../context/ApplicationContextAssert.java | 2 +- ...ter.java => ApplicationContextRunner.java} | 16 +- .../context/AssertableApplicationContext.java | 2 +- .../AssertableWebApplicationContext.java | 2 +- .../boot/test/context/ContextConsumer.java | 2 +- ... ReactiveWebApplicationContextRunner.java} | 16 +- ....java => WebApplicationContextRunner.java} | 16 +- ...bstractApplicationContextRunnerTests.java} | 6 +- ...ava => ApplicationContextRunnerTests.java} | 10 +- ...tiveWebApplicationContextRunnerTests.java} | 10 +- ... => WebApplicationContextRunnerTests.java} | 10 +- 28 files changed, 868 insertions(+), 812 deletions(-) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{AbstractApplicationContextTester.java => AbstractApplicationContextRunner.java} (90%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ApplicationContextTester.java => ApplicationContextRunner.java} (76%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ReactiveWebApplicationContextTester.java => ReactiveWebApplicationContextRunner.java} (73%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{WebApplicationContextTester.java => WebApplicationContextRunner.java} (83%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{AbstractApplicationContextTesterTests.java => AbstractApplicationContextRunnerTests.java} (96%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ApplicationContextTesterTests.java => ApplicationContextRunnerTests.java} (78%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ReactiveWebApplicationContextTesterTests.java => ReactiveWebApplicationContextRunnerTests.java} (71%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{WebApplicationContextTesterTests.java => WebApplicationContextRunnerTests.java} (78%) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java index 91dc8a7833..79683c81dd 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java @@ -60,7 +60,7 @@ import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.boot.test.context.ContextConsumer; import org.springframework.context.annotation.Bean; @@ -84,39 +84,39 @@ import static org.mockito.Mockito.mock; */ public class HealthIndicatorAutoConfigurationTests { - public final ApplicationContextTester context = new ApplicationContextTester() + public final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration( AutoConfigurations.of(HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class)); @Test public void defaultHealthIndicator() { - this.context.withPropertyValues("management.health.diskspace.enabled:false") + this.contextRunner.withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); } @Test public void defaultHealthIndicatorsDisabled() { - this.context.withPropertyValues("management.health.defaults.enabled:false") + this.contextRunner.withPropertyValues("management.health.defaults.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); } @Test public void defaultHealthIndicatorsDisabledWithCustomOne() { - this.context.withUserConfiguration(CustomHealthIndicator.class) + this.contextRunner.withUserConfiguration(CustomHealthIndicator.class) .withPropertyValues("management.health.defaults.enabled:false") - .run((loaded) -> { - Map beans = loaded + .run((context) -> { + Map beans = context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); - assertThat(loaded.getBean("customHealthIndicator")) + assertThat(context.getBean("customHealthIndicator")) .isSameAs(beans.values().iterator().next()); }); } @Test public void defaultHealthIndicatorsDisabledButOne() { - this.context + this.contextRunner .withPropertyValues("management.health.defaults.enabled:false", "management.health.diskspace.enabled:true") .run(hasSingleHealthIndicator(DiskSpaceHealthIndicator.class)); @@ -124,7 +124,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void redisHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(RedisHealthIndicator.class)); @@ -132,7 +132,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notRedisHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)) .withPropertyValues("management.health.redis.enabled:false", "management.health.diskspace.enabled:false") @@ -141,7 +141,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void mongoHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") @@ -150,7 +150,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notMongoHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class)) .withPropertyValues("management.health.mongo.enabled:false", @@ -160,10 +160,12 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void combinedHealthIndicator() { - this.context.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, - RedisAutoConfiguration.class, MongoDataAutoConfiguration.class, - SolrAutoConfiguration.class)).run((loaded) -> { - Map beans = loaded + this.contextRunner + .withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, + RedisAutoConfiguration.class, MongoDataAutoConfiguration.class, + SolrAutoConfiguration.class)) + .run((context) -> { + Map beans = context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(4); }); @@ -171,7 +173,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void dataSourceHealthIndicator() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(DataSourceAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") @@ -180,12 +182,12 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void dataSourceHealthIndicatorWithSeveralDataSources() { - this.context + this.contextRunner .withUserConfiguration(EmbeddedDataSourceConfiguration.class, DataSourceConfig.class) .withPropertyValues("management.health.diskspace.enabled:false") - .run((loaded) -> { - Map beans = loaded + .run((context) -> { + Map beans = context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); HealthIndicator bean = beans.values().iterator().next(); @@ -197,7 +199,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void dataSourceHealthIndicatorWithAbstractRoutingDataSource() { - this.context + this.contextRunner .withUserConfiguration(EmbeddedDataSourceConfiguration.class, RoutingDatasourceConfig.class) .withPropertyValues("management.health.diskspace.enabled:false") @@ -206,15 +208,15 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void dataSourceHealthIndicatorWithCustomValidationQuery() { - this.context + this.contextRunner .withUserConfiguration(DataSourceConfig.class, DataSourcePoolMetadataProvidersConfiguration.class, HealthIndicatorAutoConfiguration.class) .withPropertyValues( "spring.datasource.test.validation-query:SELECT from FOOBAR", "management.health.diskspace.enabled:false") - .run((loaded) -> { - Map beans = loaded + .run((context) -> { + Map beans = context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); HealthIndicator healthIndicator = beans.values().iterator().next(); @@ -228,7 +230,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notDataSourceHealthIndicator() { - this.context.withUserConfiguration(EmbeddedDataSourceConfiguration.class) + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) .withPropertyValues("management.health.db.enabled:false", "management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); @@ -236,7 +238,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void rabbitHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(RabbitHealthIndicator.class)); @@ -244,7 +246,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notRabbitHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class)) .withPropertyValues("management.health.rabbit.enabled:false", "management.health.diskspace.enabled:false") @@ -253,14 +255,16 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void solrHealthIndicator() { - this.context.withConfiguration(AutoConfigurations.of(SolrAutoConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(SolrAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(SolrHealthIndicator.class)); } @Test public void notSolrHealthIndicator() { - this.context.withConfiguration(AutoConfigurations.of(SolrAutoConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(SolrAutoConfiguration.class)) .withPropertyValues("management.health.solr.enabled:false", "management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); @@ -268,12 +272,12 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void diskSpaceHealthIndicator() { - this.context.run(hasSingleHealthIndicator(DiskSpaceHealthIndicator.class)); + this.contextRunner.run(hasSingleHealthIndicator(DiskSpaceHealthIndicator.class)); } @Test public void mailHealthIndicator() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(MailSenderAutoConfiguration.class)) .withPropertyValues("spring.mail.host:smtp.acme.org", @@ -283,7 +287,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notMailHealthIndicator() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(MailSenderAutoConfiguration.class)) .withPropertyValues("spring.mail.host:smtp.acme.org", @@ -294,7 +298,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void jmsHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(JmsHealthIndicator.class)); @@ -302,7 +306,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notJmsHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class)) .withPropertyValues("management.health.jms.enabled:false", "management.health.diskspace.enabled:false") @@ -311,7 +315,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void elasticsearchHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(JestClientConfiguration.class, JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class)) @@ -323,7 +327,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void elasticsearchJestHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(JestClientConfiguration.class, JestAutoConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") @@ -333,7 +337,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notElasticsearchHealthIndicator() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(JestClientConfiguration.class, JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class)) @@ -345,7 +349,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void cassandraHealthIndicator() throws Exception { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(CassandraConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(CassandraHealthIndicator.class)); @@ -353,7 +357,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notCassandraHealthIndicator() throws Exception { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(CassandraConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false", "management.health.cassandra.enabled:false") @@ -362,7 +366,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void couchbaseHealthIndicator() throws Exception { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(CouchbaseConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(CouchbaseHealthIndicator.class)); @@ -370,7 +374,7 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void notCouchbaseHealthIndicator() throws Exception { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(CouchbaseConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false", "management.health.couchbase.enabled:false") @@ -379,14 +383,16 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void ldapHealthIndicator() throws Exception { - this.context.withConfiguration(AutoConfigurations.of(LdapConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(LdapConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(LdapHealthIndicator.class)); } @Test public void notLdapHealthIndicator() throws Exception { - this.context.withConfiguration(AutoConfigurations.of(LdapConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(LdapConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false", "management.health.ldap.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); @@ -394,14 +400,16 @@ public class HealthIndicatorAutoConfigurationTests { @Test public void neo4jHealthIndicator() throws Exception { - this.context.withConfiguration(AutoConfigurations.of(Neo4jConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(Neo4jConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false") .run(hasSingleHealthIndicator(Neo4jHealthIndicator.class)); } @Test public void notNeo4jHealthIndicator() throws Exception { - this.context.withConfiguration(AutoConfigurations.of(Neo4jConfiguration.class)) + this.contextRunner + .withConfiguration(AutoConfigurations.of(Neo4jConfiguration.class)) .withPropertyValues("management.health.diskspace.enabled:false", "management.health.neo4j.enabled:false") .run(hasSingleHealthIndicator(ApplicationHealthIndicator.class)); @@ -409,8 +417,8 @@ public class HealthIndicatorAutoConfigurationTests { private ContextConsumer hasSingleHealthIndicator( Class type) { - return (loaded) -> { - assertThat(loaded).getBeans(HealthIndicator.class).hasSize(1) + return (context) -> { + assertThat(context).getBeans(HealthIndicator.class).hasSize(1) .hasValueSatisfying(new Condition<>( (indicator) -> indicator.getClass().equals(type), "Wrong indicator type")); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 9e75542dae..44fba0a485 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -54,7 +54,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.boot.test.context.ContextConsumer; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; @@ -100,58 +100,59 @@ public class CacheAutoConfigurationTests { @Rule public final ExpectedException thrown = ExpectedException.none(); - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class)); @Test public void noEnableCaching() { - this.context.withUserConfiguration(EmptyConfiguration.class).run((loaded) -> { - assertThat(loaded).doesNotHaveBean(CacheManager.class); - }); + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .run((context) -> { + assertThat(context).doesNotHaveBean(CacheManager.class); + }); } @Test public void cacheManagerBackOff() { - this.context.withUserConfiguration(CustomCacheManagerConfiguration.class) - .run((loaded) -> { - assertThat(getCacheManager(loaded, ConcurrentMapCacheManager.class) + this.contextRunner.withUserConfiguration(CustomCacheManagerConfiguration.class) + .run((context) -> { + assertThat(getCacheManager(context, ConcurrentMapCacheManager.class) .getCacheNames()).containsOnly("custom1"); }); } @Test public void cacheManagerFromSupportBackOff() { - this.context + this.contextRunner .withUserConfiguration(CustomCacheManagerFromSupportConfiguration.class) - .run((loaded) -> { - assertThat(getCacheManager(loaded, ConcurrentMapCacheManager.class) + .run((context) -> { + assertThat(getCacheManager(context, ConcurrentMapCacheManager.class) .getCacheNames()).containsOnly("custom1"); }); } @Test public void cacheResolverFromSupportBackOff() throws Exception { - this.context + this.contextRunner .withUserConfiguration(CustomCacheResolverFromSupportConfiguration.class) - .run((loaded) -> { - assertThat(loaded).doesNotHaveBean(CacheManager.class); + .run((context) -> { + assertThat(context).doesNotHaveBean(CacheManager.class); }); } @Test public void customCacheResolverCanBeDefined() throws Exception { - this.context.withUserConfiguration(SpecificCacheResolverConfiguration.class) - .withPropertyValues("spring.cache.type=simple").run((loaded) -> { - getCacheManager(loaded, ConcurrentMapCacheManager.class); - assertThat(loaded).getBeans(CacheResolver.class).hasSize(1); + this.contextRunner.withUserConfiguration(SpecificCacheResolverConfiguration.class) + .withPropertyValues("spring.cache.type=simple").run((context) -> { + getCacheManager(context, ConcurrentMapCacheManager.class); + assertThat(context).getBeans(CacheResolver.class).hasSize(1); }); } @Test public void notSupportedCachingMode() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=foobar").run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=foobar").run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining( "Failed to bind properties under 'spring.cache.type'"); @@ -160,28 +161,29 @@ public class CacheAutoConfigurationTests { @Test public void simpleCacheExplicit() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=simple").run((loaded) -> { - assertThat(getCacheManager(loaded, ConcurrentMapCacheManager.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=simple").run((context) -> { + assertThat(getCacheManager(context, ConcurrentMapCacheManager.class) .getCacheNames()).isEmpty(); }); } @Test public void simpleCacheWithCustomizers() { - this.context.withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "simple") .run(dunno("allCacheManagerCustomizer", "simpleCacheManagerCustomizer")); } @Test public void simpleCacheExplicitWithCacheNames() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=simple", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - ConcurrentMapCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + ConcurrentMapCacheManager cacheManager = getCacheManager(context, ConcurrentMapCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); }); @@ -189,23 +191,23 @@ public class CacheAutoConfigurationTests { @Test public void genericCacheWithCaches() { - this.context.withUserConfiguration(GenericCacheConfiguration.class) - .run((loaded) -> { - SimpleCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(GenericCacheConfiguration.class) + .run((context) -> { + SimpleCacheManager cacheManager = getCacheManager(context, SimpleCacheManager.class); assertThat(cacheManager.getCache("first")) - .isEqualTo(loaded.getBean("firstCache")); + .isEqualTo(context.getBean("firstCache")); assertThat(cacheManager.getCache("second")) - .isEqualTo(loaded.getBean("secondCache")); + .isEqualTo(context.getBean("secondCache")); assertThat(cacheManager.getCacheNames()).hasSize(2); }); } @Test public void genericCacheExplicit() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=generic").run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=generic").run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining( "No cache manager could be auto-configured") @@ -215,30 +217,31 @@ public class CacheAutoConfigurationTests { @Test public void genericCacheWithCustomizers() { - this.context.withUserConfiguration(GenericCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(GenericCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "generic") .run(dunno("allCacheManagerCustomizer", "genericCacheManagerCustomizer")); } @Test public void genericCacheExplicitWithCaches() { - this.context.withUserConfiguration(GenericCacheConfiguration.class) - .withPropertyValues("spring.cache.type=generic").run((loaded) -> { - SimpleCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(GenericCacheConfiguration.class) + .withPropertyValues("spring.cache.type=generic").run((context) -> { + SimpleCacheManager cacheManager = getCacheManager(context, SimpleCacheManager.class); assertThat(cacheManager.getCache("first")) - .isEqualTo(loaded.getBean("firstCache")); + .isEqualTo(context.getBean("firstCache")); assertThat(cacheManager.getCache("second")) - .isEqualTo(loaded.getBean("secondCache")); + .isEqualTo(context.getBean("secondCache")); assertThat(cacheManager.getCacheNames()).hasSize(2); }); } @Test public void couchbaseCacheExplicit() { - this.context.withUserConfiguration(CouchbaseCacheConfiguration.class) - .withPropertyValues("spring.cache.type=couchbase").run((loaded) -> { - CouchbaseCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class) + .withPropertyValues("spring.cache.type=couchbase").run((context) -> { + CouchbaseCacheManager cacheManager = getCacheManager(context, CouchbaseCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); }); @@ -246,7 +249,7 @@ public class CacheAutoConfigurationTests { @Test public void couchbaseCacheWithCustomizers() { - this.context + this.contextRunner .withUserConfiguration(CouchbaseCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "couchbase").run(dunno( "allCacheManagerCustomizer", "couchbaseCacheManagerCustomizer")); @@ -254,45 +257,45 @@ public class CacheAutoConfigurationTests { @Test public void couchbaseCacheExplicitWithCaches() { - this.context.withUserConfiguration(CouchbaseCacheConfiguration.class) + this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class) .withPropertyValues("spring.cache.type=couchbase", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - CouchbaseCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + CouchbaseCacheManager cacheManager = getCacheManager(context, CouchbaseCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); Cache cache = cacheManager.getCache("foo"); assertThat(cache).isInstanceOf(CouchbaseCache.class); assertThat(((CouchbaseCache) cache).getTtl()).isEqualTo(0); assertThat(((CouchbaseCache) cache).getNativeCache()) - .isEqualTo(loaded.getBean("bucket")); + .isEqualTo(context.getBean("bucket")); }); } @Test public void couchbaseCacheExplicitWithTtl() { - this.context.withUserConfiguration(CouchbaseCacheConfiguration.class) + this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class) .withPropertyValues("spring.cache.type=couchbase", "spring.cache.cacheNames=foo,bar", "spring.cache.couchbase.expiration=2000") - .run((loaded) -> { - CouchbaseCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + CouchbaseCacheManager cacheManager = getCacheManager(context, CouchbaseCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); Cache cache = cacheManager.getCache("foo"); assertThat(cache).isInstanceOf(CouchbaseCache.class); assertThat(((CouchbaseCache) cache).getTtl()).isEqualTo(2); assertThat(((CouchbaseCache) cache).getNativeCache()) - .isEqualTo(loaded.getBean("bucket")); + .isEqualTo(context.getBean("bucket")); }); } @Test public void redisCacheExplicit() { - this.context.withUserConfiguration(RedisCacheConfiguration.class) - .withPropertyValues("spring.cache.type=redis").run((loaded) -> { - RedisCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(RedisCacheConfiguration.class) + .withPropertyValues("spring.cache.type=redis").run((context) -> { + RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); assertThat( @@ -304,19 +307,20 @@ public class CacheAutoConfigurationTests { @Test public void redisCacheWithCustomizers() { - this.context.withUserConfiguration(RedisCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(RedisCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "redis") .run(dunno("allCacheManagerCustomizer", "redisCacheManagerCustomizer")); } @Test public void redisCacheExplicitWithCaches() { - this.context.withUserConfiguration(RedisCacheConfiguration.class) + this.contextRunner.withUserConfiguration(RedisCacheConfiguration.class) .withPropertyValues("spring.cache.type=redis", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - RedisCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); }); @@ -324,9 +328,9 @@ public class CacheAutoConfigurationTests { @Test public void noOpCacheExplicit() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=none").run((loaded) -> { - NoOpCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=none").run((context) -> { + NoOpCacheManager cacheManager = getCacheManager(context, NoOpCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); }); @@ -334,9 +338,9 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheNoProviderExplicit() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=jcache").run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=jcache").run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining( "No cache manager could be auto-configured") @@ -347,14 +351,14 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheWithProvider() { String cachingProviderFqn = MockCachingProvider.class.getName(); - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn) - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); - assertThat(loaded.getBean(javax.cache.CacheManager.class)) + assertThat(context.getBean(javax.cache.CacheManager.class)) .isEqualTo(cacheManager.getCacheManager()); }); } @@ -362,13 +366,13 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheWithCaches() { String cachingProviderFqn = MockCachingProvider.class.getName(); - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); }); @@ -377,16 +381,16 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheWithCachesAndCustomConfig() { String cachingProviderFqn = MockCachingProvider.class.getName(); - this.context.withUserConfiguration(JCacheCustomConfiguration.class) + this.contextRunner.withUserConfiguration(JCacheCustomConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=one", "spring.cache.cacheNames[1]=two") - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("one", "two"); - CompleteConfiguration defaultCacheConfiguration = loaded + CompleteConfiguration defaultCacheConfiguration = context .getBean(CompleteConfiguration.class); verify(cacheManager.getCacheManager()).createCache("one", defaultCacheConfiguration); @@ -397,23 +401,23 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheWithExistingJCacheManager() { - this.context.withUserConfiguration(JCacheCustomCacheManager.class) - .withPropertyValues("spring.cache.type=jcache").run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(JCacheCustomCacheManager.class) + .withPropertyValues("spring.cache.type=jcache").run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheManager()) - .isEqualTo(loaded.getBean("customJCacheCacheManager")); + .isEqualTo(context.getBean("customJCacheCacheManager")); }); } @Test public void jCacheCacheWithUnknownProvider() { String wrongCachingProviderClassName = "org.acme.FooBar"; - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + wrongCachingProviderClassName) - .run((loaded) -> { - assertThat(loaded).getFailure() + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining(wrongCachingProviderClassName); }); @@ -423,12 +427,12 @@ public class CacheAutoConfigurationTests { public void jCacheCacheWithConfig() { String cachingProviderFqn = MockCachingProvider.class.getName(); String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - this.context.withUserConfiguration(JCacheCustomConfiguration.class) + this.contextRunner.withUserConfiguration(JCacheCustomConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation) - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); Resource configResource = new ClassPathResource(configLocation); assertThat(cacheManager.getCacheManager().getURI()) @@ -440,12 +444,12 @@ public class CacheAutoConfigurationTests { public void jCacheCacheWithWrongConfig() { String cachingProviderFqn = MockCachingProvider.class.getName(); String configLocation = "org/springframework/boot/autoconfigure/cache/does-not-exist.xml"; - this.context.withUserConfiguration(JCacheCustomConfiguration.class) + this.contextRunner.withUserConfiguration(JCacheCustomConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation) - .run((loaded) -> { - assertThat(loaded).getFailure() + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining("does not exist") .hasMessageContaining(configLocation); @@ -454,31 +458,32 @@ public class CacheAutoConfigurationTests { @Test public void ehcacheCacheWithCaches() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=ehcache").run((loaded) -> { - EhCacheCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=ehcache").run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("cacheTest1", "cacheTest2"); - assertThat(loaded.getBean(net.sf.ehcache.CacheManager.class)) + assertThat(context.getBean(net.sf.ehcache.CacheManager.class)) .isEqualTo(cacheManager.getCacheManager()); }); } @Test public void ehcacheCacheWithCustomizers() { - this.context.withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "ehcache") .run(dunno("allCacheManagerCustomizer", "ehcacheCacheManagerCustomizer")); } @Test public void ehcacheCacheWithConfig() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=ehcache", "spring.cache.ehcache.config=cache/ehcache-override.xml") - .run((loaded) -> { - EhCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class); assertThat(cacheManager.getCacheNames()) .containsOnly("cacheOverrideTest1", "cacheOverrideTest2"); @@ -487,25 +492,25 @@ public class CacheAutoConfigurationTests { @Test public void ehcacheCacheWithExistingCacheManager() { - this.context.withUserConfiguration(EhCacheCustomCacheManager.class) - .withPropertyValues("spring.cache.type=ehcache").run((loaded) -> { - EhCacheCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(EhCacheCustomCacheManager.class) + .withPropertyValues("spring.cache.type=ehcache").run((context) -> { + EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class); assertThat(cacheManager.getCacheManager()) - .isEqualTo(loaded.getBean("customEhCacheCacheManager")); + .isEqualTo(context.getBean("customEhCacheCacheManager")); }); } @Test public void ehcache3AsJCacheWithCaches() { String cachingProviderFqn = EhcacheCachingProvider.class.getName(); - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); }); @@ -515,12 +520,12 @@ public class CacheAutoConfigurationTests { public void ehcache3AsJCacheWithConfig() throws IOException { String cachingProviderFqn = EhcacheCachingProvider.class.getName(); String configLocation = "ehcache3.xml"; - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation) - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); Resource configResource = new ClassPathResource(configLocation); @@ -532,25 +537,25 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastCacheExplicit() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(HazelcastAutoConfiguration.class)) .withUserConfiguration(DefaultCacheConfiguration.class) - .withPropertyValues("spring.cache.type=hazelcast").run((loaded) -> { - HazelcastCacheManager cacheManager = getCacheManager(loaded, + .withPropertyValues("spring.cache.type=hazelcast").run((context) -> { + HazelcastCacheManager cacheManager = getCacheManager(context, HazelcastCacheManager.class); // NOTE: the hazelcast implementation knows about a cache in a lazy // manner. cacheManager.getCache("defaultCache"); assertThat(cacheManager.getCacheNames()).containsOnly("defaultCache"); - assertThat(loaded.getBean(HazelcastInstance.class)) + assertThat(context.getBean(HazelcastInstance.class)) .isEqualTo(cacheManager.getHazelcastInstance()); }); } @Test public void hazelcastCacheWithCustomizers() { - this.context + this.contextRunner .withUserConfiguration(HazelcastCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "hazelcast").run(dunno( "allCacheManagerCustomizer", "hazelcastCacheManagerCustomizer")); @@ -558,28 +563,28 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastCacheWithExistingHazelcastInstance() { - this.context.withUserConfiguration(HazelcastCustomHazelcastInstance.class) - .withPropertyValues("spring.cache.type=hazelcast").run((loaded) -> { - HazelcastCacheManager cacheManager = getCacheManager(loaded, + this.contextRunner.withUserConfiguration(HazelcastCustomHazelcastInstance.class) + .withPropertyValues("spring.cache.type=hazelcast").run((context) -> { + HazelcastCacheManager cacheManager = getCacheManager(context, HazelcastCacheManager.class); assertThat(cacheManager.getHazelcastInstance()) - .isEqualTo(loaded.getBean("customHazelcastInstance")); + .isEqualTo(context.getBean("customHazelcastInstance")); }); } @Test public void hazelcastCacheWithHazelcastAutoConfiguration() throws IOException { String hazelcastConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(HazelcastAutoConfiguration.class)) .withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=hazelcast", "spring.hazelcast.config=" + hazelcastConfig) - .run((loaded) -> { - HazelcastCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + HazelcastCacheManager cacheManager = getCacheManager(context, HazelcastCacheManager.class); - HazelcastInstance hazelcastInstance = loaded + HazelcastInstance hazelcastInstance = context .getBean(HazelcastInstance.class); assertThat(cacheManager.getHazelcastInstance()) .isSameAs(hazelcastInstance); @@ -594,13 +599,13 @@ public class CacheAutoConfigurationTests { public void hazelcastAsJCacheWithCaches() { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); try { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); @@ -617,12 +622,12 @@ public class CacheAutoConfigurationTests { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); try { String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation) - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); Resource configResource = new ClassPathResource(configLocation); assertThat(cacheManager.getCacheManager().getURI()) @@ -638,21 +643,22 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastAsJCacheWithExistingHazelcastInstance() throws IOException { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(HazelcastAutoConfiguration.class)) .withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn) - .run((loaded) -> { - JCacheCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class); javax.cache.CacheManager jCacheManager = cacheManager .getCacheManager(); assertThat(jCacheManager).isInstanceOf( com.hazelcast.cache.HazelcastCacheManager.class); - assertThat(loaded.getBeansOfType(HazelcastInstance.class)).hasSize(1); - HazelcastInstance hazelcastInstance = loaded + assertThat(context.getBeansOfType(HazelcastInstance.class)) + .hasSize(1); + HazelcastInstance hazelcastInstance = context .getBean(HazelcastInstance.class); assertThat(((com.hazelcast.cache.HazelcastCacheManager) jCacheManager) .getHazelcastInstance()).isSameAs(hazelcastInstance); @@ -663,11 +669,11 @@ public class CacheAutoConfigurationTests { @Test public void infinispanCacheWithConfig() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=infinispan", "spring.cache.infinispan.config=infinispan.xml") - .run((loaded) -> { - SpringEmbeddedCacheManager cacheManager = getCacheManager(loaded, + .run((context) -> { + SpringEmbeddedCacheManager cacheManager = getCacheManager(context, SpringEmbeddedCacheManager.class); assertThat(cacheManager.getCacheNames()).contains("foo", "bar"); }); @@ -675,46 +681,47 @@ public class CacheAutoConfigurationTests { @Test public void infinispanCacheWithCustomizers() { - this.context.withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "infinispan").run(dunno( "allCacheManagerCustomizer", "infinispanCacheManagerCustomizer")); } @Test public void infinispanCacheWithCaches() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=infinispan", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - assertThat(getCacheManager(loaded, SpringEmbeddedCacheManager.class) + .run((context) -> { + assertThat(getCacheManager(context, SpringEmbeddedCacheManager.class) .getCacheNames()).containsOnly("foo", "bar"); }); } @Test public void infinispanCacheWithCachesAndCustomConfig() { - this.context.withUserConfiguration(InfinispanCustomConfiguration.class) + this.contextRunner.withUserConfiguration(InfinispanCustomConfiguration.class) .withPropertyValues("spring.cache.type=infinispan", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - assertThat(getCacheManager(loaded, SpringEmbeddedCacheManager.class) + .run((context) -> { + assertThat(getCacheManager(context, SpringEmbeddedCacheManager.class) .getCacheNames()).containsOnly("foo", "bar"); - verify(loaded.getBean(ConfigurationBuilder.class), times(2)).build(); + verify(context.getBean(ConfigurationBuilder.class), times(2)).build(); }); } @Test public void infinispanAsJCacheWithCaches() { String cachingProviderClassName = JCachingProvider.class.getName(); - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderClassName, "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { - assertThat(getCacheManager(loaded, JCacheCacheManager.class) + .run((context) -> { + assertThat(getCacheManager(context, JCacheCacheManager.class) .getCacheNames()).containsOnly("foo", "bar"); }); } @@ -723,13 +730,13 @@ public class CacheAutoConfigurationTests { public void infinispanAsJCacheWithConfig() throws IOException { String cachingProviderClassName = JCachingProvider.class.getName(); String configLocation = "infinispan.xml"; - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderClassName, "spring.cache.jcache.config=" + configLocation) - .run((loaded) -> { + .run((context) -> { Resource configResource = new ClassPathResource(configLocation); - assertThat(getCacheManager(loaded, JCacheCacheManager.class) + assertThat(getCacheManager(context, JCacheCacheManager.class) .getCacheManager().getURI()) .isEqualTo(configResource.getURI()); }); @@ -739,14 +746,15 @@ public class CacheAutoConfigurationTests { public void jCacheCacheWithCachesAndCustomizer() { String cachingProviderClassName = HazelcastCachingProvider.class.getName(); try { - this.context.withUserConfiguration(JCacheWithCustomizerConfiguration.class) + this.contextRunner + .withUserConfiguration(JCacheWithCustomizerConfiguration.class) .withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderClassName, "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") - .run((loaded) -> { + .run((context) -> { // see customizer - assertThat(getCacheManager(loaded, JCacheCacheManager.class) + assertThat(getCacheManager(context, JCacheCacheManager.class) .getCacheNames()).containsOnly("foo", "custom1"); }); } @@ -757,11 +765,11 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheWithExplicitCaches() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=caffeine", "spring.cache.cacheNames=foo") - .run((loaded) -> { - CaffeineCacheManager manager = getCacheManager(loaded, + .run((context) -> { + CaffeineCacheManager manager = getCacheManager(context, CaffeineCacheManager.class); assertThat(manager.getCacheNames()).containsOnly("foo"); Cache foo = manager.getCache("foo"); @@ -774,14 +782,15 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheWithCustomizers() { - this.context.withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) + this.contextRunner + .withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class) .withPropertyValues("spring.cache.type=" + "caffeine").run(dunno( "allCacheManagerCustomizer", "caffeineCacheManagerCustomizer")); } @Test public void caffeineCacheWithExplicitCacheBuilder() { - this.context.withUserConfiguration(CaffeineCacheBuilderConfiguration.class) + this.contextRunner.withUserConfiguration(CaffeineCacheBuilderConfiguration.class) .withPropertyValues("spring.cache.type=caffeine", "spring.cache.cacheNames=foo,bar") .run(this::validateCaffeineCacheWithStats); @@ -789,7 +798,7 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheExplicitWithSpec() { - this.context.withUserConfiguration(CaffeineCacheSpecConfiguration.class) + this.contextRunner.withUserConfiguration(CaffeineCacheSpecConfiguration.class) .withPropertyValues("spring.cache.type=caffeine", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar") @@ -798,7 +807,7 @@ public class CacheAutoConfigurationTests { @Test public void caffeineCacheExplicitWithSpecString() { - this.context.withUserConfiguration(DefaultCacheConfiguration.class) + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) .withPropertyValues("spring.cache.type=caffeine", "spring.cache.caffeine.spec=recordStats", "spring.cache.cacheNames[0]=foo", @@ -819,11 +828,11 @@ public class CacheAutoConfigurationTests { @SuppressWarnings("rawtypes") private ContextConsumer dunno( String... expectedCustomizerNames) { - return (loaded) -> { - CacheManager cacheManager = getCacheManager(loaded, CacheManager.class); + return (context) -> { + CacheManager cacheManager = getCacheManager(context, CacheManager.class); List expected = new ArrayList<>( Arrays.asList(expectedCustomizerNames)); - Map customizer = loaded + Map customizer = context .getBeansOfType(CacheManagerTestCustomizer.class); customizer.forEach((key, value) -> { if (expected.contains(key)) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java index 27b26346ba..47c64753cc 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java @@ -29,7 +29,7 @@ import org.springframework.boot.autoconfigure.data.neo4j.city.City; import org.springframework.boot.autoconfigure.data.neo4j.country.Country; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration; -import org.springframework.boot.test.context.WebApplicationContextTester; +import org.springframework.boot.test.context.WebApplicationContextRunner; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -54,30 +54,31 @@ import static org.mockito.Mockito.verify; */ public class Neo4jDataAutoConfigurationTests { - private WebApplicationContextTester context = new WebApplicationContextTester() + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withUserConfiguration(TestConfiguration.class) .withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class, TransactionAutoConfiguration.class)); @Test public void defaultConfiguration() { - this.context.withPropertyValues("spring.data.neo4j.uri=http://localhost:8989") - .run((loaded) -> { - assertThat(loaded) + this.contextRunner + .withPropertyValues("spring.data.neo4j.uri=http://localhost:8989") + .run((context) -> { + assertThat(context) .hasSingleBean(org.neo4j.ogm.config.Configuration.class); - assertThat(loaded).hasSingleBean(SessionFactory.class); - assertThat(loaded).hasSingleBean(Neo4jTransactionManager.class); - assertThat(loaded).hasSingleBean(OpenSessionInViewInterceptor.class); + assertThat(context).hasSingleBean(SessionFactory.class); + assertThat(context).hasSingleBean(Neo4jTransactionManager.class); + assertThat(context).hasSingleBean(OpenSessionInViewInterceptor.class); }); } @Test public void customNeo4jTransactionManagerUsingProperties() { - this.context + this.contextRunner .withPropertyValues("spring.transaction.default-timeout=30", "spring.transaction.rollback-on-commit-failure:true") - .run((loaded) -> { - Neo4jTransactionManager transactionManager = loaded + .run((context) -> { + Neo4jTransactionManager transactionManager = context .getBean(Neo4jTransactionManager.class); assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30); assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue(); @@ -86,20 +87,24 @@ public class Neo4jDataAutoConfigurationTests { @Test public void customSessionFactory() { - this.context.withUserConfiguration(CustomSessionFactory.class).run((loaded) -> { - assertThat(loaded).doesNotHaveBean(org.neo4j.ogm.config.Configuration.class); - assertThat(loaded).hasSingleBean(SessionFactory.class); - }); + this.contextRunner.withUserConfiguration(CustomSessionFactory.class) + .run((context) -> { + assertThat(context) + .doesNotHaveBean(org.neo4j.ogm.config.Configuration.class); + assertThat(context).hasSingleBean(SessionFactory.class); + }); } @Test public void customConfiguration() { - this.context.withUserConfiguration(CustomConfiguration.class).run((loaded) -> { - assertThat(loaded.getBean(org.neo4j.ogm.config.Configuration.class)) - .isSameAs(loaded.getBean("myConfiguration")); - assertThat(loaded).hasSingleBean(SessionFactory.class); - assertThat(loaded).hasSingleBean(org.neo4j.ogm.config.Configuration.class); - }); + this.contextRunner.withUserConfiguration(CustomConfiguration.class) + .run((context) -> { + assertThat(context.getBean(org.neo4j.ogm.config.Configuration.class)) + .isSameAs(context.getBean("myConfiguration")); + assertThat(context).hasSingleBean(SessionFactory.class); + assertThat(context) + .hasSingleBean(org.neo4j.ogm.config.Configuration.class); + }); } @@ -122,21 +127,21 @@ public class Neo4jDataAutoConfigurationTests { @Test public void openSessionInViewInterceptorCanBeDisabled() { - this.context.withPropertyValues("spring.data.neo4j.open-in-view:false") - .run((loaded) -> assertThat(loaded) + this.contextRunner.withPropertyValues("spring.data.neo4j.open-in-view:false") + .run((context) -> assertThat(context) .doesNotHaveBean(OpenSessionInViewInterceptor.class)); } @Test public void eventListenersAreAutoRegistered() { - this.context.withUserConfiguration(EventListenerConfiguration.class) - .run((loaded) -> { - Session session = loaded.getBean(SessionFactory.class).openSession(); + this.contextRunner.withUserConfiguration(EventListenerConfiguration.class) + .run((context) -> { + Session session = context.getBean(SessionFactory.class).openSession(); session.notifyListeners( new PersistenceEvent(null, Event.TYPE.PRE_SAVE)); - verify(loaded.getBean("eventListenerOne", EventListener.class)) + verify(context.getBean("eventListenerOne", EventListener.class)) .onPreSave(any(Event.class)); - verify(loaded.getBean("eventListenerTwo", EventListener.class)) + verify(context.getBean("eventListenerTwo", EventListener.class)) .onPreSave(any(Event.class)); }); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index f62c7e635d..d5f49f28ac 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -30,7 +30,7 @@ import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -61,17 +61,17 @@ public class HazelcastAutoConfigurationClientTests { } } - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)); @Test public void systemProperty() throws IOException { - this.context + this.contextRunner .withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY + "=classpath:org/springframework/boot/autoconfigure/hazelcast/" + "hazelcast-client-specific.xml") - .run((loaded) -> { - assertThat(loaded).getBean(HazelcastInstance.class) + .run((context) -> { + assertThat(context).getBean(HazelcastInstance.class) .isInstanceOf(HazelcastInstance.class) .has(nameStartingWith("hz.client_")); }); @@ -79,12 +79,12 @@ public class HazelcastAutoConfigurationClientTests { @Test public void explicitConfigFile() throws IOException { - this.context + this.contextRunner .withPropertyValues( "spring.hazelcast.config=org/springframework/boot/autoconfigure/" + "hazelcast/hazelcast-client-specific.xml") - .run((loaded) -> { - assertThat(loaded).getBean(HazelcastInstance.class) + .run((context) -> { + assertThat(context).getBean(HazelcastInstance.class) .isInstanceOf(HazelcastClientProxy.class) .has(nameStartingWith("hz.client_")); }); @@ -92,11 +92,11 @@ public class HazelcastAutoConfigurationClientTests { @Test public void explicitConfigUrl() throws IOException { - this.context + this.contextRunner .withPropertyValues( "spring.hazelcast.config=hazelcast-client-default.xml") - .run((loaded) -> { - assertThat(loaded).getBean(HazelcastInstance.class) + .run((context) -> { + assertThat(context).getBean(HazelcastInstance.class) .isInstanceOf(HazelcastClientProxy.class) .has(nameStartingWith("hz.client_")); }); @@ -104,9 +104,10 @@ public class HazelcastAutoConfigurationClientTests { @Test public void unknownConfigFile() { - this.context.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml") - .run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner + .withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml") + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining("foo/bar/unknown.xml"); }); @@ -114,10 +115,10 @@ public class HazelcastAutoConfigurationClientTests { @Test public void clientConfigTakesPrecedence() { - this.context.withUserConfiguration(HazelcastServerAndClientConfig.class) + this.contextRunner.withUserConfiguration(HazelcastServerAndClientConfig.class) .withPropertyValues("spring.hazelcast.config=this-is-ignored.xml") - .run((loaded) -> { - assertThat(loaded).getBean(HazelcastInstance.class) + .run((context) -> { + assertThat(context).getBean(HazelcastInstance.class) .isInstanceOf(HazelcastClientProxy.class); }); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index 756dfafa58..c6714e5190 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java @@ -28,7 +28,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.context.annotation.Bean; @@ -46,14 +46,14 @@ import static org.assertj.core.api.Assertions.assertThat; @ClassPathExclusions("hazelcast-client-*.jar") public class HazelcastAutoConfigurationServerTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)); @Test public void defaultConfigFile() throws IOException { // hazelcast.xml present in root classpath - this.context.run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + this.contextRunner.run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getConfigurationUrl()) .isEqualTo(new ClassPathResource("hazelcast.xml").getURL()); }); @@ -61,22 +61,22 @@ public class HazelcastAutoConfigurationServerTests { @Test public void systemProperty() throws IOException { - this.context + this.contextRunner .withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml") - .run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + .run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getQueueConfigs().keySet()).containsOnly("foobar"); }); } @Test public void explicitConfigFile() throws IOException { - this.context.withPropertyValues( + this.contextRunner.withPropertyValues( "spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + "hazelcast-specific.xml") - .run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + .run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getConfigurationFile()) .isEqualTo(new ClassPathResource( "org/springframework/boot/autoconfigure/hazelcast" @@ -86,9 +86,10 @@ public class HazelcastAutoConfigurationServerTests { @Test public void explicitConfigUrl() throws IOException { - this.context.withPropertyValues("spring.hazelcast.config=hazelcast-default.xml") - .run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + this.contextRunner + .withPropertyValues("spring.hazelcast.config=hazelcast-default.xml") + .run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getConfigurationUrl()).isEqualTo( new ClassPathResource("hazelcast-default.xml").getURL()); }); @@ -96,9 +97,10 @@ public class HazelcastAutoConfigurationServerTests { @Test public void unknownConfigFile() { - this.context.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml") - .run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner + .withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml") + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining("foo/bar/unknown.xml"); }); @@ -109,10 +111,10 @@ public class HazelcastAutoConfigurationServerTests { Config config = new Config("my-test-instance"); HazelcastInstance existing = Hazelcast.newHazelcastInstance(config); try { - this.context.withUserConfiguration(HazelcastConfigWithName.class) + this.contextRunner.withUserConfiguration(HazelcastConfigWithName.class) .withPropertyValues("spring.hazelcast.config=this-is-ignored.xml") - .run(loaded -> { - HazelcastInstance hazelcast = (loaded) + .run((context) -> { + HazelcastInstance hazelcast = context .getBean(HazelcastInstance.class); assertThat(hazelcast.getConfig().getInstanceName()) .isEqualTo("my-test-instance"); @@ -127,10 +129,10 @@ public class HazelcastAutoConfigurationServerTests { @Test public void configInstanceWithoutName() { - this.context.withUserConfiguration(HazelcastConfigNoName.class) + this.contextRunner.withUserConfiguration(HazelcastConfigNoName.class) .withPropertyValues("spring.hazelcast.config=this-is-ignored.xml") - .run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + .run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); Map queueConfigs = config.getQueueConfigs(); assertThat(queueConfigs.keySet()).containsOnly("another-queue"); }); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java index c074207884..c296b73569 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java @@ -23,7 +23,7 @@ import com.hazelcast.core.HazelcastInstance; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.core.io.ClassPathResource; import static org.assertj.core.api.Assertions.assertThat; @@ -35,14 +35,14 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class HazelcastAutoConfigurationTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)); @Test public void defaultConfigFile() throws IOException { // no hazelcast-client.xml and hazelcast.xml is present in root classpath - this.context.run((loaded) -> { - Config config = loaded.getBean(HazelcastInstance.class).getConfig(); + this.contextRunner.run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getConfigurationUrl()) .isEqualTo(new ClassPathResource("hazelcast.xml").getURL()); }); 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 553af58d4d..cc102d93d8 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 @@ -25,7 +25,7 @@ import retrofit2.Retrofit; import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,59 +41,60 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class InfluxDbAutoConfigurationTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class)); @Test public void influxDbRequiresUrl() { - this.context.run( - (loaded) -> assertThat(loaded.getBeansOfType(InfluxDB.class)).isEmpty()); + this.contextRunner + .run((context) -> assertThat(context.getBeansOfType(InfluxDB.class)) + .isEmpty()); } @Test public void influxDbCanBeCustomized() { - this.context + this.contextRunner .withPropertyValues("spring.influx.url=http://localhost", "spring.influx.password:password", "spring.influx.user:user") - .run((loaded -> assertThat(loaded.getBeansOfType(InfluxDB.class)) + .run(((context) -> assertThat(context.getBeansOfType(InfluxDB.class)) .hasSize(1))); } @Test public void influxDbCanBeCreatedWithoutCredentials() { - this.context.withPropertyValues("spring.influx.url=http://localhost") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(loaded); + this.contextRunner.withPropertyValues("spring.influx.url=http://localhost") + .run((context) -> { + assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(10_000); }); } @Test public void influxDbWithoutCredentialsAndOkHttpClientBuilder() { - this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) .withPropertyValues("spring.influx.url=http://localhost") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(loaded); + .run((context) -> { + assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(30_000); }); } @Test public void influxDbWithOkHttpClientBuilder() { - this.context.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) + this.contextRunner.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); + .run((context) -> { + assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(30_000); }); } - private int getReadTimeoutProperty(AssertableApplicationContext loaded) { - InfluxDB influxDB = loaded.getBean(InfluxDB.class); + private int getReadTimeoutProperty(AssertableApplicationContext context) { + InfluxDB influxDB = context.getBean(InfluxDB.class); Retrofit retrofit = (Retrofit) new DirectFieldAccessor(influxDB) .getPropertyValue("retrofit"); OkHttpClient callFactory = (OkHttpClient) new DirectFieldAccessor(retrofit) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index f452b954c4..8b0e5a4188 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -39,7 +39,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.jdbc.DatabaseDriver; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.boot.test.context.HidePackagesClassLoader; import org.springframework.context.annotation.Bean; @@ -58,7 +58,7 @@ import static org.mockito.Mockito.mock; */ public class DataSourceAutoConfigurationTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class)) .withPropertyValues("spring.datasource.initialize=false", "spring.datasource.url:jdbc:hsqldb:mem:testdb-" @@ -66,13 +66,14 @@ public class DataSourceAutoConfigurationTests { @Test public void testDefaultDataSourceExists() throws Exception { - this.context.run((loaded) -> assertThat(loaded).hasSingleBean(DataSource.class)); + this.contextRunner + .run((context) -> assertThat(context).hasSingleBean(DataSource.class)); } @Test public void testDataSourceHasEmbeddedDefault() throws Exception { - this.context.run((loaded) -> { - HikariDataSource dataSource = loaded.getBean(HikariDataSource.class); + this.contextRunner.run((context) -> { + HikariDataSource dataSource = context.getBean(HikariDataSource.class); assertThat(dataSource.getJdbcUrl()).isNotNull(); assertThat(dataSource.getDriverClassName()).isNotNull(); }); @@ -82,10 +83,10 @@ public class DataSourceAutoConfigurationTests { public void testBadUrl() throws Exception { try { EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE; - this.context + this.contextRunner .withPropertyValues("spring.datasource.url:jdbc:not-going-to-work") - .run((loaded) -> { - assertThat(loaded).getFailure() + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class); }); } @@ -96,11 +97,11 @@ public class DataSourceAutoConfigurationTests { @Test public void testBadDriverClass() throws Exception { - this.context + this.contextRunner .withPropertyValues( "spring.datasource.driverClassName:org.none.jdbcDriver") - .run((loaded) -> { - assertThat(loaded).getFailure() + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining("org.none.jdbcDriver"); }); @@ -153,10 +154,10 @@ public class DataSourceAutoConfigurationTests { @Test @SuppressWarnings("resource") public void testEmbeddedTypeDefaultsUsername() throws Exception { - this.context.withPropertyValues( + this.contextRunner.withPropertyValues( "spring.datasource.driverClassName:org.hsqldb.jdbcDriver", - "spring.datasource.url:jdbc:hsqldb:mem:testdb").run((loaded) -> { - DataSource bean = loaded.getBean(DataSource.class); + "spring.datasource.url:jdbc:hsqldb:mem:testdb").run((context) -> { + DataSource bean = context.getBean(DataSource.class); HikariDataSource pool = (HikariDataSource) bean; assertThat(pool.getDriverClassName()) .isEqualTo("org.hsqldb.jdbcDriver"); @@ -170,7 +171,7 @@ public class DataSourceAutoConfigurationTests { */ @Test public void explicitTypeNoSupportedDataSource() { - this.context + this.contextRunner .withClassLoader(new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari", "org.apache.commons.dbcp", "org.apache.commons.dbcp2")) @@ -184,7 +185,7 @@ public class DataSourceAutoConfigurationTests { @Test public void explicitTypeSupportedDataSource() { - this.context + this.contextRunner .withPropertyValues( "spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.url:jdbc:hsqldb:mem:testdb", @@ -193,19 +194,20 @@ public class DataSourceAutoConfigurationTests { .run(this::containsOnlySimpleDriverDataSource); } - private void containsOnlySimpleDriverDataSource(AssertableApplicationContext loaded) { - assertThat(loaded).hasSingleBean(DataSource.class); - assertThat(loaded).getBean(DataSource.class) + private void containsOnlySimpleDriverDataSource( + AssertableApplicationContext context) { + assertThat(context).hasSingleBean(DataSource.class); + assertThat(context).getBean(DataSource.class) .isExactlyInstanceOf(SimpleDriverDataSource.class); } @Test public void testExplicitDriverClassClearsUsername() throws Exception { - this.context.withPropertyValues( + this.contextRunner.withPropertyValues( "spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(), - "spring.datasource.url:jdbc:foo://localhost").run((loaded) -> { - assertThat(loaded).hasSingleBean(DataSource.class); - HikariDataSource dataSource = loaded.getBean(HikariDataSource.class); + "spring.datasource.url:jdbc:foo://localhost").run((context) -> { + assertThat(context).hasSingleBean(DataSource.class); + HikariDataSource dataSource = context.getBean(HikariDataSource.class); assertThat(dataSource.getDriverClassName()) .isEqualTo(DatabaseTestDriver.class.getName()); assertThat(dataSource.getUsername()).isNull(); @@ -214,18 +216,19 @@ public class DataSourceAutoConfigurationTests { @Test public void testDefaultDataSourceCanBeOverridden() throws Exception { - this.context.withUserConfiguration(TestDataSourceConfiguration.class) - .run((loaded) -> { - assertThat(loaded).getBean(DataSource.class) + this.contextRunner.withUserConfiguration(TestDataSourceConfiguration.class) + .run((context) -> { + assertThat(context).getBean(DataSource.class) .isInstanceOf(BasicDataSource.class); }); } @Test public void testDataSourceIsInitializedEarly() { - this.context.withUserConfiguration(TestInitializedDataSourceConfiguration.class) + this.contextRunner + .withUserConfiguration(TestInitializedDataSourceConfiguration.class) .withPropertyValues("spring.datasource.initialize=true") - .run((loaded) -> assertThat(loaded + .run((context) -> assertThat(context .getBean(TestInitializedDataSourceConfiguration.class).called) .isTrue()); } @@ -234,8 +237,8 @@ public class DataSourceAutoConfigurationTests { List hiddenPackages, Consumer consumer) { HidePackagesClassLoader classLoader = new HidePackagesClassLoader( hiddenPackages.toArray(new String[hiddenPackages.size()])); - this.context.withClassLoader(classLoader).run((loaded) -> { - DataSource bean = loaded.getBean(DataSource.class); + this.contextRunner.withClassLoader(classLoader).run((context) -> { + DataSource bean = context.getBean(DataSource.class); assertThat(bean).isInstanceOf(expectedType); consumer.accept(expectedType.cast(bean)); }); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index e16efb821b..b23769eb99 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -28,7 +28,7 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -63,13 +63,13 @@ public class JmsAutoConfigurationTests { private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616"; - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class)); @Test public void testDefaultJmsConfiguration() { - this.context.withUserConfiguration(TestConfiguration.class) + this.contextRunner.withUserConfiguration(TestConfiguration.class) .run(this::testDefaultJmsConfiguration); } @@ -88,29 +88,29 @@ public class JmsAutoConfigurationTests { @Test public void testConnectionFactoryBackOff() { - this.context.withUserConfiguration(TestConfiguration2.class) - .run((loaded) -> assertThat( - loaded.getBean(ActiveMQConnectionFactory.class).getBrokerURL()) + this.contextRunner.withUserConfiguration(TestConfiguration2.class) + .run((context) -> assertThat( + context.getBean(ActiveMQConnectionFactory.class).getBrokerURL()) .isEqualTo("foobar")); } @Test public void testJmsTemplateBackOff() { - this.context.withUserConfiguration(TestConfiguration3.class).run( - (loaded) -> assertThat(loaded.getBean(JmsTemplate.class).getPriority()) + this.contextRunner.withUserConfiguration(TestConfiguration3.class).run( + (context) -> assertThat(context.getBean(JmsTemplate.class).getPriority()) .isEqualTo(999)); } @Test public void testJmsMessagingTemplateBackOff() { - this.context.withUserConfiguration(TestConfiguration5.class) - .run((loaded) -> assertThat(loaded.getBean(JmsMessagingTemplate.class) + this.contextRunner.withUserConfiguration(TestConfiguration5.class) + .run((context) -> assertThat(context.getBean(JmsMessagingTemplate.class) .getDefaultDestinationName()).isEqualTo("fooBar")); } @Test public void testJmsTemplateBackOffEverything() { - this.context + this.contextRunner .withUserConfiguration(TestConfiguration2.class, TestConfiguration3.class, TestConfiguration5.class) .run(this::testJmsTemplateBackOffEverything); @@ -129,8 +129,8 @@ public class JmsAutoConfigurationTests { @Test public void testEnableJmsCreateDefaultContainerFactory() { - this.context.withUserConfiguration(EnableJmsConfiguration.class) - .run((loaded) -> assertThat(loaded) + this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class) + .run((context) -> assertThat(context) .getBean("jmsListenerContainerFactory", JmsListenerContainerFactory.class) .isExactlyInstanceOf(DefaultJmsListenerContainerFactory.class)); @@ -138,10 +138,10 @@ public class JmsAutoConfigurationTests { @Test public void testJmsListenerContainerFactoryBackOff() { - this.context + this.contextRunner .withUserConfiguration(TestConfiguration6.class, EnableJmsConfiguration.class) - .run((loaded) -> assertThat(loaded) + .run((context) -> assertThat(context) .getBean("jmsListenerContainerFactory", JmsListenerContainerFactory.class) .isExactlyInstanceOf(SimpleJmsListenerContainerFactory.class)); @@ -149,7 +149,7 @@ public class JmsAutoConfigurationTests { @Test public void testJmsListenerContainerFactoryWithCustomSettings() { - this.context.withUserConfiguration(EnableJmsConfiguration.class) + this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class) .withPropertyValues("spring.jms.listener.autoStartup=false", "spring.jms.listener.acknowledgeMode=client", "spring.jms.listener.concurrency=2", @@ -170,22 +170,22 @@ public class JmsAutoConfigurationTests { @Test public void testDefaultContainerFactoryWithJtaTransactionManager() { - this.context.withUserConfiguration(TestConfiguration7.class, - EnableJmsConfiguration.class).run((loaded) -> { - DefaultMessageListenerContainer container = getContainer(loaded, + this.contextRunner.withUserConfiguration(TestConfiguration7.class, + EnableJmsConfiguration.class).run((context) -> { + DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory"); assertThat(container.isSessionTransacted()).isFalse(); assertThat(new DirectFieldAccessor(container) .getPropertyValue("transactionManager")).isSameAs( - loaded.getBean(JtaTransactionManager.class)); + context.getBean(JtaTransactionManager.class)); }); } @Test public void testDefaultContainerFactoryNonJtaTransactionManager() { - this.context.withUserConfiguration(TestConfiguration8.class, - EnableJmsConfiguration.class).run((loaded) -> { - DefaultMessageListenerContainer container = getContainer(loaded, + this.contextRunner.withUserConfiguration(TestConfiguration8.class, + EnableJmsConfiguration.class).run((context) -> { + DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory"); assertThat(container.isSessionTransacted()).isTrue(); assertThat(new DirectFieldAccessor(container) @@ -195,34 +195,35 @@ public class JmsAutoConfigurationTests { @Test public void testDefaultContainerFactoryNoTransactionManager() { - this.context.withUserConfiguration(EnableJmsConfiguration.class).run((loaded) -> { - DefaultMessageListenerContainer container = getContainer(loaded, - "jmsListenerContainerFactory"); - assertThat(container.isSessionTransacted()).isTrue(); - assertThat(new DirectFieldAccessor(container) - .getPropertyValue("transactionManager")).isNull(); - }); + this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class) + .run((context) -> { + DefaultMessageListenerContainer container = getContainer(context, + "jmsListenerContainerFactory"); + assertThat(container.isSessionTransacted()).isTrue(); + assertThat(new DirectFieldAccessor(container) + .getPropertyValue("transactionManager")).isNull(); + }); } @Test public void testDefaultContainerFactoryWithMessageConverters() { - this.context.withUserConfiguration(MessageConvertersConfiguration.class, - EnableJmsConfiguration.class).run((loaded) -> { - DefaultMessageListenerContainer container = getContainer(loaded, + this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class, + EnableJmsConfiguration.class).run((context) -> { + DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory"); assertThat(container.getMessageConverter()) - .isSameAs(loaded.getBean("myMessageConverter")); + .isSameAs(context.getBean("myMessageConverter")); }); } @Test public void testCustomContainerFactoryWithConfigurer() { - this.context + this.contextRunner .withUserConfiguration(TestConfiguration9.class, EnableJmsConfiguration.class) .withPropertyValues("spring.jms.listener.autoStartup=false") - .run((loaded) -> { - DefaultMessageListenerContainer container = getContainer(loaded, + .run((context) -> { + DefaultMessageListenerContainer container = getContainer(context, "customListenerContainerFactory"); assertThat(container.getCacheLevel()) .isEqualTo(DefaultMessageListenerContainer.CACHE_CONSUMER); @@ -241,35 +242,35 @@ public class JmsAutoConfigurationTests { @Test public void testJmsTemplateWithMessageConverter() { - this.context.withUserConfiguration(MessageConvertersConfiguration.class) - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); + this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class) + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); assertThat(jmsTemplate.getMessageConverter()) - .isSameAs(loaded.getBean("myMessageConverter")); + .isSameAs(context.getBean("myMessageConverter")); }); } @Test public void testJmsTemplateWithDestinationResolver() { - this.context.withUserConfiguration(DestinationResolversConfiguration.class) - .run((loaded) -> assertThat( - loaded.getBean(JmsTemplate.class).getDestinationResolver()) - .isSameAs(loaded.getBean("myDestinationResolver"))); + this.contextRunner.withUserConfiguration(DestinationResolversConfiguration.class) + .run((context) -> assertThat( + context.getBean(JmsTemplate.class).getDestinationResolver()) + .isSameAs(context.getBean("myDestinationResolver"))); } @Test public void testJmsTemplateFullCustomization() { - this.context.withUserConfiguration(MessageConvertersConfiguration.class) + this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class) .withPropertyValues("spring.jms.template.default-destination=testQueue", "spring.jms.template.delivery-delay=500", "spring.jms.template.delivery-mode=non-persistent", "spring.jms.template.priority=6", "spring.jms.template.time-to-live=6000", "spring.jms.template.receive-timeout=2000") - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); assertThat(jmsTemplate.getMessageConverter()) - .isSameAs(loaded.getBean("myMessageConverter")); + .isSameAs(context.getBean("myMessageConverter")); assertThat(jmsTemplate.isPubSubDomain()).isFalse(); assertThat(jmsTemplate.getDefaultDestinationName()) .isEqualTo("testQueue"); @@ -284,24 +285,24 @@ public class JmsAutoConfigurationTests { @Test public void testPubSubDisabledByDefault() { - this.context.withUserConfiguration(TestConfiguration.class).run( - (loaded) -> assertThat(loaded.getBean(JmsTemplate.class).isPubSubDomain()) - .isFalse()); + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .run((context) -> assertThat( + context.getBean(JmsTemplate.class).isPubSubDomain()).isFalse()); } @Test public void testJmsTemplatePostProcessedSoThatPubSubIsTrue() { - this.context.withUserConfiguration(TestConfiguration4.class).run( - (loaded) -> assertThat(loaded.getBean(JmsTemplate.class).isPubSubDomain()) - .isTrue()); + this.contextRunner.withUserConfiguration(TestConfiguration4.class) + .run((context) -> assertThat( + context.getBean(JmsTemplate.class).isPubSubDomain()).isTrue()); } @Test public void testPubSubDomainActive() { - this.context.withUserConfiguration(TestConfiguration.class) - .withPropertyValues("spring.jms.pubSubDomain:true").run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - DefaultMessageListenerContainer defaultMessageListenerContainer = loaded + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.jms.pubSubDomain:true").run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + DefaultMessageListenerContainer defaultMessageListenerContainer = context .getBean(DefaultJmsListenerContainerFactory.class) .createListenerContainer(mock(JmsListenerEndpoint.class)); assertThat(jmsTemplate.isPubSubDomain()).isTrue(); @@ -311,10 +312,10 @@ public class JmsAutoConfigurationTests { @Test public void testPubSubDomainOverride() { - this.context.withUserConfiguration(TestConfiguration.class) - .withPropertyValues("spring.jms.pubSubDomain:false").run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - ActiveMQConnectionFactory factory = loaded + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.jms.pubSubDomain:false").run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(jmsTemplate.isPubSubDomain()).isFalse(); @@ -325,10 +326,10 @@ public class JmsAutoConfigurationTests { @Test public void testActiveMQOverriddenStandalone() { - this.context.withUserConfiguration(TestConfiguration.class) - .withPropertyValues("spring.activemq.inMemory:false").run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - ActiveMQConnectionFactory factory = loaded + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.activemq.inMemory:false").run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(factory).isNotNull() @@ -341,11 +342,11 @@ public class JmsAutoConfigurationTests { @Test public void testActiveMQOverriddenRemoteHost() { - this.context.withUserConfiguration(TestConfiguration.class) + this.contextRunner.withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.activemq.brokerUrl:tcp://remote-host:10000") - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - ActiveMQConnectionFactory factory = loaded + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(factory).isNotNull(); @@ -358,10 +359,11 @@ public class JmsAutoConfigurationTests { @Test public void testActiveMQOverriddenPool() { - this.context.withUserConfiguration(TestConfiguration.class) - .withPropertyValues("spring.activemq.pool.enabled:true").run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - PooledConnectionFactory pool = loaded + this.contextRunner.withUserConfiguration(TestConfiguration.class) + .withPropertyValues("spring.activemq.pool.enabled:true") + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context .getBean(PooledConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(pool).isNotNull(); @@ -374,12 +376,12 @@ public class JmsAutoConfigurationTests { @Test public void testActiveMQOverriddenPoolAndStandalone() { - this.context.withUserConfiguration(TestConfiguration.class) + this.contextRunner.withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.activemq.pool.enabled:true", "spring.activemq.inMemory:false") - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - PooledConnectionFactory pool = loaded + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context .getBean(PooledConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(pool).isNotNull(); @@ -392,12 +394,12 @@ public class JmsAutoConfigurationTests { @Test public void testActiveMQOverriddenPoolAndRemoteServer() { - this.context.withUserConfiguration(TestConfiguration.class) + this.contextRunner.withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.activemq.pool.enabled:true", "spring.activemq.brokerUrl:tcp://remote-host:10000") - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - PooledConnectionFactory pool = loaded + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + PooledConnectionFactory pool = context .getBean(PooledConnectionFactory.class); assertThat(jmsTemplate).isNotNull(); assertThat(pool).isNotNull(); @@ -411,9 +413,9 @@ public class JmsAutoConfigurationTests { @Test public void enableJmsAutomatically() throws Exception { - this.context.withUserConfiguration(NoEnableJmsConfiguration.class) - .run((loaded) -> { - assertThat(loaded) + this.contextRunner.withUserConfiguration(NoEnableJmsConfiguration.class) + .run((context) -> { + assertThat(context) .hasBean( JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME) .hasBean( diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java index c5f3a80788..4c3a492f2d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -42,36 +42,39 @@ import static org.mockito.Mockito.mockingDetails; */ public class ActiveMQAutoConfigurationTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class)); @Test public void brokerIsEmbeddedByDefault() { - this.context.withUserConfiguration(EmptyConfiguration.class).run((loaded) -> { - assertThat(loaded).getBean(ConnectionFactory.class) - .isInstanceOf(ActiveMQConnectionFactory.class); - assertThat(loaded.getBean(ActiveMQConnectionFactory.class).getBrokerURL()) - .isEqualTo("vm://localhost?broker.persistent=false"); - }); + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .run((context) -> { + assertThat(context).getBean(ConnectionFactory.class) + .isInstanceOf(ActiveMQConnectionFactory.class); + assertThat(context.getBean(ActiveMQConnectionFactory.class) + .getBrokerURL()) + .isEqualTo("vm://localhost?broker.persistent=false"); + }); } @Test public void configurationBacksOffWhenCustomConnectionFactoryExists() { - this.context.withUserConfiguration(CustomConnectionFactoryConfiguration.class) - .run((loaded) -> assertThat( - mockingDetails(loaded.getBean(ConnectionFactory.class)).isMock()) + this.contextRunner + .withUserConfiguration(CustomConnectionFactoryConfiguration.class) + .run((context) -> assertThat( + mockingDetails(context.getBean(ConnectionFactory.class)).isMock()) .isTrue()); } @Test public void defaultsConnectionFactoryAreApplied() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.activemq.pool.enabled=false") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(ActiveMQConnectionFactory.class)) + .run((context) -> { + assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)) .hasSize(1); - ActiveMQConnectionFactory connectionFactory = loaded + ActiveMQConnectionFactory connectionFactory = context .getBean(ActiveMQConnectionFactory.class); ActiveMQConnectionFactory defaultFactory = new ActiveMQConnectionFactory( "vm://localhost?broker.persistent=false"); @@ -94,7 +97,7 @@ public class ActiveMQAutoConfigurationTests { @Test public void customConnectionFactoryAreApplied() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.activemq.pool.enabled=false", "spring.activemq.brokerUrl=vm://localhost?useJmx=false&broker.persistent=false", "spring.activemq.user=foo", "spring.activemq.password=bar", @@ -103,10 +106,10 @@ public class ActiveMQAutoConfigurationTests { "spring.activemq.sendTimeout=1000", "spring.activemq.packages.trust-all=false", "spring.activemq.packages.trusted=com.example.acme") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(ActiveMQConnectionFactory.class)) + .run((context) -> { + assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)) .hasSize(1); - ActiveMQConnectionFactory connectionFactory = loaded + ActiveMQConnectionFactory connectionFactory = context .getBean(ActiveMQConnectionFactory.class); assertThat(connectionFactory.getUserName()).isEqualTo("foo"); assertThat(connectionFactory.getPassword()).isEqualTo("bar"); @@ -122,11 +125,12 @@ public class ActiveMQAutoConfigurationTests { @Test public void defaultsPooledConnectionFactoryAreApplied() { - this.context.withUserConfiguration(EmptyConfiguration.class) - .withPropertyValues("spring.activemq.pool.enabled=true").run((loaded) -> { - assertThat(loaded.getBeansOfType(PooledConnectionFactory.class)) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .withPropertyValues("spring.activemq.pool.enabled=true") + .run((context) -> { + assertThat(context.getBeansOfType(PooledConnectionFactory.class)) .hasSize(1); - PooledConnectionFactory connectionFactory = loaded + PooledConnectionFactory connectionFactory = context .getBean(PooledConnectionFactory.class); PooledConnectionFactory defaultFactory = new PooledConnectionFactory(); assertThat(connectionFactory.isBlockIfSessionPoolIsFull()) @@ -157,7 +161,7 @@ public class ActiveMQAutoConfigurationTests { @Test public void customPooledConnectionFactoryAreApplied() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.activemq.pool.enabled=true", "spring.activemq.pool.blockIfFull=false", "spring.activemq.pool.blockIfFullTimeout=64", @@ -169,10 +173,10 @@ public class ActiveMQAutoConfigurationTests { "spring.activemq.pool.reconnectOnException=false", "spring.activemq.pool.timeBetweenExpirationCheck=2048", "spring.activemq.pool.useAnonymousProducers=false") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(PooledConnectionFactory.class)) + .run((context) -> { + assertThat(context.getBeansOfType(PooledConnectionFactory.class)) .hasSize(1); - PooledConnectionFactory connectionFactory = loaded + PooledConnectionFactory connectionFactory = context .getBean(PooledConnectionFactory.class); assertThat(connectionFactory.isBlockIfSessionPoolIsFull()) .isEqualTo(false); @@ -196,11 +200,12 @@ public class ActiveMQAutoConfigurationTests { @Test public void pooledConnectionFactoryConfiguration() throws JMSException { - this.context.withUserConfiguration(EmptyConfiguration.class) - .withPropertyValues("spring.activemq.pool.enabled:true").run((loaded) -> { - ConnectionFactory factory = loaded.getBean(ConnectionFactory.class); + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .withPropertyValues("spring.activemq.pool.enabled:true") + .run((context) -> { + ConnectionFactory factory = context.getBean(ConnectionFactory.class); assertThat(factory).isInstanceOf(PooledConnectionFactory.class); - loaded.getSourceApplicationContext().close(); + context.getSourceApplicationContext().close(); assertThat(factory.createConnection()).isNull(); }); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index 00984b43b2..edab68342c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -42,7 +42,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -64,16 +64,16 @@ public class ArtemisAutoConfigurationTests { @Rule public final TemporaryFolder folder = new TemporaryFolder(); - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(ArtemisAutoConfiguration.class, JmsAutoConfiguration.class)); @Test public void nativeConnectionFactory() { - this.context.withUserConfiguration(EmptyConfiguration.class) - .withPropertyValues("spring.artemis.mode:native").run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - ActiveMQConnectionFactory factory = loaded + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .withPropertyValues("spring.artemis.mode:native").run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertThat(factory).isEqualTo(jmsTemplate.getConnectionFactory()); assertNettyConnectionFactory(factory, "localhost", 61616); @@ -84,11 +84,11 @@ public class ArtemisAutoConfigurationTests { @Test public void nativeConnectionFactoryCustomHost() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.mode:native", "spring.artemis.host:192.168.1.144", "spring.artemis.port:9876") - .run((loaded) -> { - ActiveMQConnectionFactory factory = loaded + .run((context) -> { + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertNettyConnectionFactory(factory, "192.168.1.144", 9876); }); @@ -96,12 +96,12 @@ public class ArtemisAutoConfigurationTests { @Test public void nativeConnectionFactoryCredentials() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.mode:native", "spring.artemis.user:user", "spring.artemis.password:secret") - .run((loaded) -> { - JmsTemplate jmsTemplate = loaded.getBean(JmsTemplate.class); - ActiveMQConnectionFactory factory = loaded + .run((context) -> { + JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertThat(factory).isEqualTo(jmsTemplate.getConnectionFactory()); assertNettyConnectionFactory(factory, "localhost", 61616); @@ -112,18 +112,18 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedConnectionFactory() { - this.context.withUserConfiguration(EmptyConfiguration.class) - .withPropertyValues("spring.artemis.mode:embedded").run((loaded) -> { - ArtemisProperties properties = loaded + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .withPropertyValues("spring.artemis.mode:embedded").run((context) -> { + ArtemisProperties properties = context .getBean(ArtemisProperties.class); assertThat(properties.getMode()).isEqualTo(ArtemisMode.EMBEDDED); - assertThat(loaded).hasSingleBean(EmbeddedJMS.class); - org.apache.activemq.artemis.core.config.Configuration configuration = loaded + assertThat(context).hasSingleBean(EmbeddedJMS.class); + org.apache.activemq.artemis.core.config.Configuration configuration = context .getBean( org.apache.activemq.artemis.core.config.Configuration.class); assertThat(configuration.isPersistenceEnabled()).isFalse(); assertThat(configuration.isSecurityEnabled()).isFalse(); - ActiveMQConnectionFactory factory = loaded + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertInVmConnectionFactory(factory); }); @@ -132,26 +132,28 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedConnectionFactoryByDefault() { // No mode is specified - this.context.withUserConfiguration(EmptyConfiguration.class).run((loaded) -> { - assertThat(loaded).hasSingleBean(EmbeddedJMS.class); - org.apache.activemq.artemis.core.config.Configuration configuration = loaded - .getBean(org.apache.activemq.artemis.core.config.Configuration.class); - assertThat(configuration.isPersistenceEnabled()).isFalse(); - assertThat(configuration.isSecurityEnabled()).isFalse(); - ActiveMQConnectionFactory factory = loaded - .getBean(ActiveMQConnectionFactory.class); - assertInVmConnectionFactory(factory); - }); + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) + .run((context) -> { + assertThat(context).hasSingleBean(EmbeddedJMS.class); + org.apache.activemq.artemis.core.config.Configuration configuration = context + .getBean( + org.apache.activemq.artemis.core.config.Configuration.class); + assertThat(configuration.isPersistenceEnabled()).isFalse(); + assertThat(configuration.isSecurityEnabled()).isFalse(); + ActiveMQConnectionFactory factory = context + .getBean(ActiveMQConnectionFactory.class); + assertInVmConnectionFactory(factory); + }); } @Test public void nativeConnectionFactoryIfEmbeddedServiceDisabledExplicitly() { // No mode is specified - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.embedded.enabled:false") - .run((loaded) -> { - assertThat(loaded).doesNotHaveBean(EmbeddedJMS.class); - ActiveMQConnectionFactory factory = loaded + .run((context) -> { + assertThat(context).doesNotHaveBean(EmbeddedJMS.class); + ActiveMQConnectionFactory factory = context .getBean(ActiveMQConnectionFactory.class); assertNettyConnectionFactory(factory, "localhost", 61616); }); @@ -160,12 +162,12 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedConnectionFactoryEvenIfEmbeddedServiceDisabled() { // No mode is specified - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.mode:embedded", "spring.artemis.embedded.enabled:false") - .run((loaded) -> { - assertThat(loaded.getBeansOfType(EmbeddedJMS.class)).isEmpty(); - ActiveMQConnectionFactory connectionFactory = loaded + .run((context) -> { + assertThat(context.getBeansOfType(EmbeddedJMS.class)).isEmpty(); + ActiveMQConnectionFactory connectionFactory = context .getBean(ActiveMQConnectionFactory.class); assertInVmConnectionFactory(connectionFactory); }); @@ -173,11 +175,11 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedServerWithDestinations() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.embedded.queues=Queue1,Queue2", "spring.artemis.embedded.topics=Topic1") - .run((loaded) -> { - DestinationChecker checker = new DestinationChecker(loaded); + .run((context) -> { + DestinationChecker checker = new DestinationChecker(context); checker.checkQueue("Queue1", true); checker.checkQueue("Queue2", true); checker.checkQueue("QueueWillNotBeAutoCreated", true); @@ -188,9 +190,9 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedServerWithDestinationConfig() { - this.context.withUserConfiguration(DestinationConfiguration.class) - .run((loaded) -> { - DestinationChecker checker = new DestinationChecker(loaded); + this.contextRunner.withUserConfiguration(DestinationConfiguration.class) + .run((context) -> { + DestinationChecker checker = new DestinationChecker(context); checker.checkQueue("sampleQueue", true); checker.checkTopic("sampleTopic", true); }); @@ -199,10 +201,10 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedServiceWithCustomJmsConfiguration() { // Ignored with custom config - this.context.withUserConfiguration(CustomJmsConfiguration.class) + this.contextRunner.withUserConfiguration(CustomJmsConfiguration.class) .withPropertyValues("spring.artemis.embedded.queues=Queue1,Queue2") - .run((loaded) -> { - DestinationChecker checker = new DestinationChecker(loaded); + .run((context) -> { + DestinationChecker checker = new DestinationChecker(context); checker.checkQueue("custom", true); // See CustomJmsConfiguration checker.checkQueue("Queue1", true); checker.checkQueue("Queue2", true); @@ -211,8 +213,8 @@ public class ArtemisAutoConfigurationTests { @Test public void embeddedServiceWithCustomArtemisConfiguration() { - this.context.withUserConfiguration(CustomArtemisConfiguration.class) - .run((loaded) -> assertThat(loaded + this.contextRunner.withUserConfiguration(CustomArtemisConfiguration.class) + .run((context) -> assertThat(context .getBean( org.apache.activemq.artemis.core.config.Configuration.class) .getName()).isEqualTo("customFooBar")); @@ -223,16 +225,16 @@ public class ArtemisAutoConfigurationTests { File dataFolder = this.folder.newFolder(); final String messageId = UUID.randomUUID().toString(); // Start the server and post a message to some queue - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath()) - .run((loaded) -> loaded.getBean(JmsTemplate.class).send("TestQueue", + .run((context) -> context.getBean(JmsTemplate.class).send("TestQueue", (session) -> session.createTextMessage(messageId))); // Start the server again and check if our message is still here - this.context.run((loaded) -> { - JmsTemplate jmsTemplate2 = loaded.getBean(JmsTemplate.class); + this.contextRunner.run((context) -> { + JmsTemplate jmsTemplate2 = context.getBean(JmsTemplate.class); jmsTemplate2.setReceiveTimeout(1000L); Message message = jmsTemplate2.receive("TestQueue"); assertThat(message).isNotNull(); @@ -242,10 +244,10 @@ public class ArtemisAutoConfigurationTests { @Test public void severalEmbeddedBrokers() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.embedded.queues=Queue1") .run((first) -> { - this.context + this.contextRunner .withPropertyValues("spring.artemis.embedded.queues=Queue2") .run((second) -> { ArtemisProperties firstProperties = first @@ -266,11 +268,11 @@ public class ArtemisAutoConfigurationTests { @Test public void connectToASpecificEmbeddedBroker() { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.embedded.serverId=93", "spring.artemis.embedded.queues=Queue1") .run((first) -> { - this.context.withUserConfiguration(EmptyConfiguration.class) + this.contextRunner.withUserConfiguration(EmptyConfiguration.class) .withPropertyValues("spring.artemis.mode=embedded", // Connect to the "main" broker "spring.artemis.embedded.serverId=93", diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java index 6780aac54f..850d81570c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java @@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ReactiveWebApplicationContextTester; +import org.springframework.boot.test.context.ReactiveWebApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.server.reactive.HttpHandler; @@ -39,24 +39,25 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class HttpHandlerAutoConfigurationTests { - private final ReactiveWebApplicationContextTester context = new ReactiveWebApplicationContextTester() + private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(HttpHandlerAutoConfiguration.class)); @Test public void shouldNotProcessIfExistingHttpHandler() { - this.context.withUserConfiguration(CustomHttpHandler.class).run((loaded) -> { - assertThat(loaded).hasSingleBean(HttpHandler.class); - assertThat(loaded).getBean(HttpHandler.class) - .isSameAs(loaded.getBean("customHttpHandler")); - }); + this.contextRunner.withUserConfiguration(CustomHttpHandler.class) + .run((context) -> { + assertThat(context).hasSingleBean(HttpHandler.class); + assertThat(context).getBean(HttpHandler.class) + .isSameAs(context.getBean("customHttpHandler")); + }); } @Test public void shouldConfigureHttpHandlerAnnotation() { - this.context + this.contextRunner .withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class)) - .run((loaded) -> { - assertThat(loaded).hasSingleBean(HttpHandler.class); + .run((context) -> { + assertThat(context).hasSingleBean(HttpHandler.class); }); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 32244f65db..1bc41ca63c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -40,7 +40,7 @@ import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguratio import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WelcomePageHandlerMapping; import org.springframework.boot.test.context.AssertableWebApplicationContext; import org.springframework.boot.test.context.ContextConsumer; -import org.springframework.boot.test.context.WebApplicationContextTester; +import org.springframework.boot.test.context.WebApplicationContextRunner; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; @@ -116,7 +116,7 @@ public class WebMvcAutoConfigurationTests { private static final MockServletWebServerFactory webServerFactory = new MockServletWebServerFactory(); - private final WebApplicationContextTester context = new WebApplicationContextTester() + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class)) @@ -124,50 +124,50 @@ public class WebMvcAutoConfigurationTests { @Test public void handlerAdaptersCreated() { - this.context.run((loaded) -> { - assertThat(loaded).getBeans(HandlerAdapter.class).hasSize(3); - assertThat(loaded.getBean(RequestMappingHandlerAdapter.class) + this.contextRunner.run((context) -> { + assertThat(context).getBeans(HandlerAdapter.class).hasSize(3); + assertThat(context.getBean(RequestMappingHandlerAdapter.class) .getMessageConverters()).isNotEmpty().isEqualTo( - loaded.getBean(HttpMessageConverters.class).getConverters()); + context.getBean(HttpMessageConverters.class).getConverters()); }); } @Test public void handlerMappingsCreated() { - this.context.run( - (loaded) -> assertThat(loaded).getBeans(HandlerMapping.class).hasSize(7)); + this.contextRunner.run((context) -> assertThat(context) + .getBeans(HandlerMapping.class).hasSize(7)); } @Test public void resourceHandlerMapping() { - this.context.run((loaded) -> { - Map> locations = getResourceMappingLocations(loaded); + this.contextRunner.run((context) -> { + Map> locations = getResourceMappingLocations(context); assertThat(locations.get("/**")).hasSize(5); assertThat(locations.get("/webjars/**")).hasSize(1); assertThat(locations.get("/webjars/**").get(0)) .isEqualTo(new ClassPathResource("/META-INF/resources/webjars/")); - assertThat(getResourceResolvers(loaded, "/webjars/**")).hasSize(1); - assertThat(getResourceTransformers(loaded, "/webjars/**")).hasSize(0); - assertThat(getResourceResolvers(loaded, "/**")).hasSize(1); - assertThat(getResourceTransformers(loaded, "/**")).hasSize(0); + assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(1); + assertThat(getResourceTransformers(context, "/webjars/**")).hasSize(0); + assertThat(getResourceResolvers(context, "/**")).hasSize(1); + assertThat(getResourceTransformers(context, "/**")).hasSize(0); }); } @Test public void customResourceHandlerMapping() { - this.context.withPropertyValues("spring.mvc.static-path-pattern:/static/**") - .run((loaded) -> { + this.contextRunner.withPropertyValues("spring.mvc.static-path-pattern:/static/**") + .run((context) -> { Map> locations = getResourceMappingLocations( - loaded); + context); assertThat(locations.get("/static/**")).hasSize(5); - assertThat(getResourceResolvers(loaded, "/static/**")).hasSize(1); + assertThat(getResourceResolvers(context, "/static/**")).hasSize(1); }); } @Test public void resourceHandlerMappingOverrideWebjars() throws Exception { - this.context.withUserConfiguration(WebJars.class).run((loaded) -> { - Map> locations = getResourceMappingLocations(loaded); + this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> { + Map> locations = getResourceMappingLocations(context); assertThat(locations.get("/webjars/**")).hasSize(1); assertThat(locations.get("/webjars/**").get(0)) .isEqualTo(new ClassPathResource("/foo/")); @@ -176,8 +176,8 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerMappingOverrideAll() throws Exception { - this.context.withUserConfiguration(AllResources.class).run((loaded) -> { - Map> locations = getResourceMappingLocations(loaded); + this.contextRunner.withUserConfiguration(AllResources.class).run((context) -> { + Map> locations = getResourceMappingLocations(context); assertThat(locations.get("/**")).hasSize(1); assertThat(locations.get("/**").get(0)) .isEqualTo(new ClassPathResource("/foo/")); @@ -186,25 +186,26 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerMappingDisabled() throws Exception { - this.context.withPropertyValues("spring.resources.add-mappings:false") - .run((loaded) -> { + this.contextRunner.withPropertyValues("spring.resources.add-mappings:false") + .run((context) -> { Map> locations = getResourceMappingLocations( - loaded); + context); assertThat(locations.size()).isEqualTo(0); }); } @Test public void resourceHandlerChainEnabled() throws Exception { - this.context.withPropertyValues("spring.resources.chain.enabled:true") - .run((loaded) -> { - assertThat(getResourceResolvers(loaded, "/webjars/**")).hasSize(2); - assertThat(getResourceTransformers(loaded, "/webjars/**")).hasSize(1); - assertThat(getResourceResolvers(loaded, "/**")) + this.contextRunner.withPropertyValues("spring.resources.chain.enabled:true") + .run((context) -> { + assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(2); + assertThat(getResourceTransformers(context, "/webjars/**")) + .hasSize(1); + assertThat(getResourceResolvers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceResolver.class, PathResourceResolver.class); - assertThat(getResourceTransformers(loaded, "/**")) + assertThat(getResourceTransformers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceTransformer.class); }); @@ -212,24 +213,25 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerFixedStrategyEnabled() throws Exception { - this.context + this.contextRunner .withPropertyValues("spring.resources.chain.strategy.fixed.enabled:true", "spring.resources.chain.strategy.fixed.version:test", "spring.resources.chain.strategy.fixed.paths:/**/*.js") - .run((loaded) -> { - assertThat(getResourceResolvers(loaded, "/webjars/**")).hasSize(3); - assertThat(getResourceTransformers(loaded, "/webjars/**")).hasSize(2); - assertThat(getResourceResolvers(loaded, "/**")) + .run((context) -> { + assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(3); + assertThat(getResourceTransformers(context, "/webjars/**")) + .hasSize(2); + assertThat(getResourceResolvers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceResolver.class, VersionResourceResolver.class, PathResourceResolver.class); - assertThat(getResourceTransformers(loaded, "/**")) + assertThat(getResourceTransformers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceTransformer.class, CssLinkResourceTransformer.class); VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers( - loaded, "/**").get(1); + context, "/**").get(1); assertThat(resolver.getStrategyMap().get("/**/*.js")) .isInstanceOf(FixedVersionStrategy.class); }); @@ -237,24 +239,25 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerContentStrategyEnabled() throws Exception { - this.context + this.contextRunner .withPropertyValues( "spring.resources.chain.strategy.content.enabled:true", "spring.resources.chain.strategy.content.paths:/**,/*.png") - .run((loaded) -> { - assertThat(getResourceResolvers(loaded, "/webjars/**")).hasSize(3); - assertThat(getResourceTransformers(loaded, "/webjars/**")).hasSize(2); - assertThat(getResourceResolvers(loaded, "/**")) + .run((context) -> { + assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(3); + assertThat(getResourceTransformers(context, "/webjars/**")) + .hasSize(2); + assertThat(getResourceResolvers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceResolver.class, VersionResourceResolver.class, PathResourceResolver.class); - assertThat(getResourceTransformers(loaded, "/**")) + assertThat(getResourceTransformers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CachingResourceTransformer.class, CssLinkResourceTransformer.class); VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers( - loaded, "/**").get(1); + context, "/**").get(1); assertThat(resolver.getStrategyMap().get("/*.png")) .isInstanceOf(ContentVersionStrategy.class); }); @@ -262,7 +265,7 @@ public class WebMvcAutoConfigurationTests { @Test public void resourceHandlerChainCustomized() { - this.context.withPropertyValues("spring.resources.chain.enabled:true", + this.contextRunner.withPropertyValues("spring.resources.chain.enabled:true", "spring.resources.chain.cache:false", "spring.resources.chain.strategy.content.enabled:true", "spring.resources.chain.strategy.content.paths:/**,/*.png", @@ -270,20 +273,21 @@ public class WebMvcAutoConfigurationTests { "spring.resources.chain.strategy.fixed.version:test", "spring.resources.chain.strategy.fixed.paths:/**/*.js", "spring.resources.chain.html-application-cache:true", - "spring.resources.chain.gzipped:true").run((loaded) -> { - assertThat(getResourceResolvers(loaded, "/webjars/**")).hasSize(3); - assertThat(getResourceTransformers(loaded, "/webjars/**")).hasSize(2); - assertThat(getResourceResolvers(loaded, "/**")) + "spring.resources.chain.gzipped:true").run((context) -> { + assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(3); + assertThat(getResourceTransformers(context, "/webjars/**")) + .hasSize(2); + assertThat(getResourceResolvers(context, "/**")) .extractingResultOf("getClass") .containsOnly(VersionResourceResolver.class, GzipResourceResolver.class, PathResourceResolver.class); - assertThat(getResourceTransformers(loaded, "/**")) + assertThat(getResourceTransformers(context, "/**")) .extractingResultOf("getClass") .containsOnly(CssLinkResourceTransformer.class, AppCacheManifestTransformer.class); VersionResourceResolver resolver = (VersionResourceResolver) getResourceResolvers( - loaded, "/**").get(0); + context, "/**").get(0); Map strategyMap = resolver.getStrategyMap(); assertThat(strategyMap.get("/*.png")) .isInstanceOf(ContentVersionStrategy.class); @@ -294,13 +298,13 @@ public class WebMvcAutoConfigurationTests { @Test public void noLocaleResolver() throws Exception { - this.context.run( - (loaded) -> assertThat(loaded).doesNotHaveBean(LocaleResolver.class)); + this.contextRunner.run( + (context) -> assertThat(context).doesNotHaveBean(LocaleResolver.class)); } @Test public void overrideLocale() throws Exception { - this.context.withPropertyValues("spring.mvc.locale:en_UK", + this.contextRunner.withPropertyValues("spring.mvc.locale:en_UK", "spring.mvc.locale-resolver=fixed").run((loader) -> { // mock request and set user preferred locale MockHttpServletRequest request = new MockHttpServletRequest(); @@ -317,7 +321,7 @@ public class WebMvcAutoConfigurationTests { @Test public void useAcceptHeaderLocale() { - this.context.withPropertyValues("spring.mvc.locale:en_UK").run((loader) -> { + this.contextRunner.withPropertyValues("spring.mvc.locale:en_UK").run((loader) -> { // mock request and set user preferred locale MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(StringUtils.parseLocaleString("nl_NL")); @@ -332,21 +336,23 @@ public class WebMvcAutoConfigurationTests { @Test public void useDefaultLocaleIfAcceptHeaderNoSet() { - this.context.withPropertyValues("spring.mvc.locale:en_UK").run((loaded) -> { - // mock request and set user preferred locale - MockHttpServletRequest request = new MockHttpServletRequest(); - LocaleResolver localeResolver = loaded.getBean(LocaleResolver.class); - assertThat(localeResolver).isInstanceOf(AcceptHeaderLocaleResolver.class); - Locale locale = localeResolver.resolveLocale(request); - // test locale resolver uses default locale if no header is set - assertThat(locale.toString()).isEqualTo("en_UK"); - }); + this.contextRunner.withPropertyValues("spring.mvc.locale:en_UK") + .run((context) -> { + // mock request and set user preferred locale + MockHttpServletRequest request = new MockHttpServletRequest(); + LocaleResolver localeResolver = context.getBean(LocaleResolver.class); + assertThat(localeResolver) + .isInstanceOf(AcceptHeaderLocaleResolver.class); + Locale locale = localeResolver.resolveLocale(request); + // test locale resolver uses default locale if no header is set + assertThat(locale.toString()).isEqualTo("en_UK"); + }); } @Test public void noDateFormat() { - this.context.run((loaded) -> { - FormattingConversionService conversionService = loaded + this.contextRunner.run((context) -> { + FormattingConversionService conversionService = context .getBean(FormattingConversionService.class); Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); // formatting conversion service should use simple toString() @@ -357,9 +363,9 @@ public class WebMvcAutoConfigurationTests { @Test public void overrideDateFormat() { - this.context.withPropertyValues("spring.mvc.date-format:dd*MM*yyyy") - .run((loaded) -> { - FormattingConversionService conversionService = loaded + this.contextRunner.withPropertyValues("spring.mvc.date-format:dd*MM*yyyy") + .run((context) -> { + FormattingConversionService conversionService = context .getBean(FormattingConversionService.class); Date date = new DateTime(1988, 6, 25, 20, 30).toDate(); assertThat(conversionService.convert(date, String.class)) @@ -369,106 +375,107 @@ public class WebMvcAutoConfigurationTests { @Test public void noMessageCodesResolver() { - this.context.run((loaded) -> assertThat(loaded + this.contextRunner.run((context) -> assertThat(context .getBean(WebMvcAutoConfigurationAdapter.class).getMessageCodesResolver()) .isNull()); } @Test public void overrideMessageCodesFormat() { - this.context + this.contextRunner .withPropertyValues( "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE") - .run((loaded) -> assertThat( - loaded.getBean(WebMvcAutoConfigurationAdapter.class) + .run((context) -> assertThat( + context.getBean(WebMvcAutoConfigurationAdapter.class) .getMessageCodesResolver()).isNotNull()); } @Test public void ignoreDefaultModelOnRedirectIsTrue() { - this.context.run( - (loaded) -> assertThat(loaded.getBean(RequestMappingHandlerAdapter.class)) + this.contextRunner.run((context) -> assertThat( + context.getBean(RequestMappingHandlerAdapter.class)) .extracting("ignoreDefaultModelOnRedirect") .containsExactly(true)); } @Test public void overrideIgnoreDefaultModelOnRedirect() { - this.context + this.contextRunner .withPropertyValues("spring.mvc.ignore-default-model-on-redirect:false") - .run((loaded) -> assertThat( - loaded.getBean(RequestMappingHandlerAdapter.class)) + .run((context) -> assertThat( + context.getBean(RequestMappingHandlerAdapter.class)) .extracting("ignoreDefaultModelOnRedirect") .containsExactly(false)); } @Test public void customViewResolver() { - this.context.withUserConfiguration(CustomViewResolver.class) - .run((loaded) -> assertThat(loaded.getBean("viewResolver")) + this.contextRunner.withUserConfiguration(CustomViewResolver.class) + .run((context) -> assertThat(context.getBean("viewResolver")) .isInstanceOf(MyViewResolver.class)); } @Test public void customContentNegotiatingViewResolver() throws Exception { - this.context.withUserConfiguration(CustomContentNegotiatingViewResolver.class) - .run((loaded) -> assertThat(loaded) + this.contextRunner + .withUserConfiguration(CustomContentNegotiatingViewResolver.class) + .run((context) -> assertThat(context) .getBeanNames(ContentNegotiatingViewResolver.class) .containsOnly("myViewResolver")); } @Test public void faviconMapping() { - this.context.run((loaded) -> { - assertThat(loaded).getBeanNames(ResourceHttpRequestHandler.class) + this.contextRunner.run((context) -> { + assertThat(context).getBeanNames(ResourceHttpRequestHandler.class) .contains("faviconRequestHandler"); - assertThat(loaded).getBeans(SimpleUrlHandlerMapping.class) + assertThat(context).getBeans(SimpleUrlHandlerMapping.class) .containsKey("faviconHandlerMapping"); - assertThat(getFaviconMappingLocations(loaded).get("/**/favicon.ico")) + assertThat(getFaviconMappingLocations(context).get("/**/favicon.ico")) .hasSize(6); }); } @Test public void faviconMappingUsesStaticLocations() { - this.context + this.contextRunner .withPropertyValues("spring.resources.static-locations=classpath:/static") - .run((loaded) -> assertThat( - getFaviconMappingLocations(loaded).get("/**/favicon.ico")) + .run((context) -> assertThat( + getFaviconMappingLocations(context).get("/**/favicon.ico")) .hasSize(2)); } @Test public void faviconMappingDisabled() throws IllegalAccessException { - this.context.withPropertyValues("spring.mvc.favicon.enabled:false") - .run((loaded) -> { - assertThat(loaded).getBeans(ResourceHttpRequestHandler.class) + this.contextRunner.withPropertyValues("spring.mvc.favicon.enabled:false") + .run((context) -> { + assertThat(context).getBeans(ResourceHttpRequestHandler.class) .doesNotContainKey("faviconRequestHandler"); - assertThat(loaded).getBeans(SimpleUrlHandlerMapping.class) + assertThat(context).getBeans(SimpleUrlHandlerMapping.class) .doesNotContainKey("faviconHandlerMapping"); }); } @Test public void defaultAsyncRequestTimeout() throws Exception { - this.context.run((loaded) -> assertThat(ReflectionTestUtils.getField( - loaded.getBean(RequestMappingHandlerAdapter.class), + this.contextRunner.run((context) -> assertThat(ReflectionTestUtils.getField( + context.getBean(RequestMappingHandlerAdapter.class), "asyncRequestTimeout")).isNull()); } @Test public void customAsyncRequestTimeout() throws Exception { - this.context.withPropertyValues("spring.mvc.async.request-timeout:12345") - .run((loaded) -> assertThat(ReflectionTestUtils.getField( - loaded.getBean(RequestMappingHandlerAdapter.class), + this.contextRunner.withPropertyValues("spring.mvc.async.request-timeout:12345") + .run((context) -> assertThat(ReflectionTestUtils.getField( + context.getBean(RequestMappingHandlerAdapter.class), "asyncRequestTimeout")).isEqualTo(12345L)); } @Test public void customMediaTypes() throws Exception { - this.context.withPropertyValues("spring.mvc.mediaTypes.yaml:text/yaml") - .run((loaded) -> { - RequestMappingHandlerAdapter adapter = loaded + this.contextRunner.withPropertyValues("spring.mvc.mediaTypes.yaml:text/yaml") + .run((context) -> { + RequestMappingHandlerAdapter adapter = context .getBean(RequestMappingHandlerAdapter.class); ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils .getField(adapter, "contentNegotiationManager"); @@ -479,80 +486,82 @@ public class WebMvcAutoConfigurationTests { @Test public void httpPutFormContentFilterIsAutoConfigured() { - this.context.run((loaded) -> assertThat(loaded) + this.contextRunner.run((context) -> assertThat(context) .hasSingleBean(OrderedHttpPutFormContentFilter.class)); } @Test public void httpPutFormContentFilterCanBeOverridden() { - this.context.withUserConfiguration(CustomHttpPutFormContentFilter.class) - .run((loaded) -> { - assertThat(loaded) + this.contextRunner.withUserConfiguration(CustomHttpPutFormContentFilter.class) + .run((context) -> { + assertThat(context) .doesNotHaveBean(OrderedHttpPutFormContentFilter.class); - assertThat(loaded).hasSingleBean(HttpPutFormContentFilter.class); + assertThat(context).hasSingleBean(HttpPutFormContentFilter.class); }); } @Test public void httpPutFormContentFilterCanBeDisabled() throws Exception { - this.context.withPropertyValues("spring.mvc.formcontent.putfilter.enabled=false") - .run((loaded) -> assertThat(loaded) + this.contextRunner + .withPropertyValues("spring.mvc.formcontent.putfilter.enabled=false") + .run((context) -> assertThat(context) .doesNotHaveBean(HttpPutFormContentFilter.class)); } @Test public void customConfigurableWebBindingInitializer() { - this.context.withUserConfiguration(CustomConfigurableWebBindingInitializer.class) - .run((loaded) -> assertThat( - loaded.getBean(RequestMappingHandlerAdapter.class) + this.contextRunner + .withUserConfiguration(CustomConfigurableWebBindingInitializer.class) + .run((context) -> assertThat( + context.getBean(RequestMappingHandlerAdapter.class) .getWebBindingInitializer()) .isInstanceOf(CustomWebBindingInitializer.class)); } @Test public void customRequestMappingHandlerMapping() { - this.context.withUserConfiguration(CustomRequestMappingHandlerMapping.class) - .run((loaded) -> assertThat(loaded) + this.contextRunner.withUserConfiguration(CustomRequestMappingHandlerMapping.class) + .run((context) -> assertThat(context) .getBean(RequestMappingHandlerMapping.class) .isInstanceOf(MyRequestMappingHandlerMapping.class)); } @Test public void customRequestMappingHandlerAdapter() { - this.context.withUserConfiguration(CustomRequestMappingHandlerAdapter.class) - .run((loaded) -> assertThat(loaded) + this.contextRunner.withUserConfiguration(CustomRequestMappingHandlerAdapter.class) + .run((context) -> assertThat(context) .getBean(RequestMappingHandlerAdapter.class) .isInstanceOf(MyRequestMappingHandlerAdapter.class)); } @Test public void multipleWebMvcRegistrations() { - this.context.withUserConfiguration(MultipleWebMvcRegistrations.class) - .run((loaded) -> { - assertThat(loaded.getBean(RequestMappingHandlerMapping.class)) + this.contextRunner.withUserConfiguration(MultipleWebMvcRegistrations.class) + .run((context) -> { + assertThat(context.getBean(RequestMappingHandlerMapping.class)) .isNotInstanceOf(MyRequestMappingHandlerMapping.class); - assertThat(loaded.getBean(RequestMappingHandlerAdapter.class)) + assertThat(context.getBean(RequestMappingHandlerAdapter.class)) .isNotInstanceOf(MyRequestMappingHandlerAdapter.class); }); } @Test public void defaultLogResolvedException() { - this.context.run(assertExceptionResolverWarnLoggers( + this.contextRunner.run(assertExceptionResolverWarnLoggers( (logger) -> assertThat(logger).isNull())); } @Test public void customLogResolvedException() { - this.context.withPropertyValues("spring.mvc.log-resolved-exception:true") + this.contextRunner.withPropertyValues("spring.mvc.log-resolved-exception:true") .run(assertExceptionResolverWarnLoggers( (logger) -> assertThat(logger).isNotNull())); } private ContextConsumer assertExceptionResolverWarnLoggers( Consumer consumer) { - return (loaded) -> { - HandlerExceptionResolver resolver = loaded + return (context) -> { + HandlerExceptionResolver resolver = context .getBean(HandlerExceptionResolver.class); assertThat(resolver).isInstanceOf(HandlerExceptionResolverComposite.class); List delegates = ((HandlerExceptionResolverComposite) resolver) @@ -567,12 +576,12 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/no-welcome-page/") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvcBuilders.webAppContextSetup(loaded).build() + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvcBuilders.webAppContextSetup(context).build() .perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isNotFound()); }); @@ -580,23 +589,23 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageRootHandlerIsNotRegisteredWhenStaticPathPatternIsNotSlashStarStar() { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page/", "spring.mvc.static-path-pattern:/foo/**") - .run((loaded) -> assertThat( - loaded.getBean(WelcomePageHandlerMapping.class).getRootHandler()) + .run((context) -> assertThat( + context.getBean(WelcomePageHandlerMapping.class).getRootHandler()) .isNull()); } @Test public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page/") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(loaded).build(); + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); @@ -607,12 +616,12 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageMappingDoesNotHandleRequestsThatDoNotAcceptTextHtml() { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page/") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(loaded).build(); + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); }); @@ -620,12 +629,12 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page/") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(loaded).build(); + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/")).andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); }); @@ -634,12 +643,12 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageMappingHandlesRequestsWithEmptyAcceptHeader() throws Exception { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page/") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(loaded).build(); + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").header(HttpHeaders.ACCEPT, "")) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); @@ -649,12 +658,12 @@ public class WebMvcAutoConfigurationTests { @Test public void welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation() throws Exception { - this.context + this.contextRunner .withPropertyValues( "spring.resources.static-locations:classpath:/welcome-page") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(WelcomePageHandlerMapping.class); - MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(loaded).build(); + .run((context) -> { + assertThat(context).hasSingleBean(WelcomePageHandlerMapping.class); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); @@ -664,121 +673,128 @@ public class WebMvcAutoConfigurationTests { @Test public void validatorWhenNoValidatorShouldUseDefault() { - this.context.run((loaded) -> { - assertThat(loaded).doesNotHaveBean(ValidatorFactory.class); - assertThat(loaded).doesNotHaveBean(javax.validation.Validator.class); - assertThat(loaded).getBeanNames(Validator.class).containsOnly("mvcValidator"); + this.contextRunner.run((context) -> { + assertThat(context).doesNotHaveBean(ValidatorFactory.class); + assertThat(context).doesNotHaveBean(javax.validation.Validator.class); + assertThat(context).getBeanNames(Validator.class) + .containsOnly("mvcValidator"); }); } @Test public void validatorWhenNoCustomizationShouldUseAutoConfigured() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(ValidationAutoConfiguration.class)) - .run((loaded) -> { - assertThat(loaded).getBeanNames(javax.validation.Validator.class) + .run((context) -> { + assertThat(context).getBeanNames(javax.validation.Validator.class) .containsOnly("defaultValidator"); - assertThat(loaded).getBeanNames(Validator.class) + assertThat(context).getBeanNames(Validator.class) .containsOnly("defaultValidator", "mvcValidator"); - Validator validator = loaded.getBean("mvcValidator", Validator.class); + Validator validator = context.getBean("mvcValidator", + Validator.class); assertThat(validator).isInstanceOf(ValidatorAdapter.class); - Object defaultValidator = loaded.getBean("defaultValidator"); + Object defaultValidator = context.getBean("defaultValidator"); assertThat(((ValidatorAdapter) validator).getTarget()) .isSameAs(defaultValidator); // Primary Spring validator is the one used by MVC behind the scenes - assertThat(loaded.getBean(Validator.class)) + assertThat(context.getBean(Validator.class)) .isEqualTo(defaultValidator); }); } @Test public void validatorWithConfigurerShouldUseSpringValidator() { - this.context.withUserConfiguration(MvcValidator.class).run((loaded) -> { - assertThat(loaded).doesNotHaveBean(ValidatorFactory.class); - assertThat(loaded).doesNotHaveBean(javax.validation.Validator.class); - assertThat(loaded).getBeanNames(Validator.class).containsOnly("mvcValidator"); - assertThat(loaded.getBean("mvcValidator")) - .isSameAs(loaded.getBean(MvcValidator.class).validator); + this.contextRunner.withUserConfiguration(MvcValidator.class).run((context) -> { + assertThat(context).doesNotHaveBean(ValidatorFactory.class); + assertThat(context).doesNotHaveBean(javax.validation.Validator.class); + assertThat(context).getBeanNames(Validator.class) + .containsOnly("mvcValidator"); + assertThat(context.getBean("mvcValidator")) + .isSameAs(context.getBean(MvcValidator.class).validator); }); } @Test public void validatorWithConfigurerDoesNotExposeJsr303() { - this.context.withUserConfiguration(MvcJsr303Validator.class).run((loaded) -> { - assertThat(loaded).doesNotHaveBean(ValidatorFactory.class); - assertThat(loaded).doesNotHaveBean(javax.validation.Validator.class); - assertThat(loaded).getBeanNames(Validator.class).containsOnly("mvcValidator"); - Validator validator = loaded.getBean("mvcValidator", Validator.class); - assertThat(validator).isInstanceOf(ValidatorAdapter.class); - assertThat(((ValidatorAdapter) validator).getTarget()) - .isSameAs(loaded.getBean(MvcJsr303Validator.class).validator); - }); + this.contextRunner.withUserConfiguration(MvcJsr303Validator.class) + .run((context) -> { + assertThat(context).doesNotHaveBean(ValidatorFactory.class); + assertThat(context).doesNotHaveBean(javax.validation.Validator.class); + assertThat(context).getBeanNames(Validator.class) + .containsOnly("mvcValidator"); + Validator validator = context.getBean("mvcValidator", + Validator.class); + assertThat(validator).isInstanceOf(ValidatorAdapter.class); + assertThat(((ValidatorAdapter) validator).getTarget()).isSameAs( + context.getBean(MvcJsr303Validator.class).validator); + }); } @Test public void validatorWithConfigurerTakesPrecedence() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(ValidationAutoConfiguration.class)) - .withUserConfiguration(MvcValidator.class).run((loaded) -> { - assertThat(loaded).hasSingleBean(ValidatorFactory.class); - assertThat(loaded).hasSingleBean(javax.validation.Validator.class); - assertThat(loaded).getBeanNames(Validator.class) + .withUserConfiguration(MvcValidator.class).run((context) -> { + assertThat(context).hasSingleBean(ValidatorFactory.class); + assertThat(context).hasSingleBean(javax.validation.Validator.class); + assertThat(context).getBeanNames(Validator.class) .containsOnly("defaultValidator", "mvcValidator"); - assertThat(loaded.getBean("mvcValidator")) - .isSameAs(loaded.getBean(MvcValidator.class).validator); + assertThat(context.getBean("mvcValidator")) + .isSameAs(context.getBean(MvcValidator.class).validator); // Primary Spring validator is the auto-configured one as the MVC one // has been customized via a WebMvcConfigurer - assertThat(loaded.getBean(Validator.class)) - .isEqualTo(loaded.getBean("defaultValidator")); + assertThat(context.getBean(Validator.class)) + .isEqualTo(context.getBean("defaultValidator")); }); } @Test public void validatorWithCustomSpringValidatorIgnored() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(ValidationAutoConfiguration.class)) - .withUserConfiguration(CustomSpringValidator.class).run((loaded) -> { - assertThat(loaded).getBeanNames(javax.validation.Validator.class) + .withUserConfiguration(CustomSpringValidator.class).run((context) -> { + assertThat(context).getBeanNames(javax.validation.Validator.class) .containsOnly("defaultValidator"); - assertThat(loaded).getBeanNames(Validator.class).containsOnly( + assertThat(context).getBeanNames(Validator.class).containsOnly( "customSpringValidator", "defaultValidator", "mvcValidator"); - Validator validator = loaded.getBean("mvcValidator", Validator.class); + Validator validator = context.getBean("mvcValidator", + Validator.class); assertThat(validator).isInstanceOf(ValidatorAdapter.class); - Object defaultValidator = loaded.getBean("defaultValidator"); + Object defaultValidator = context.getBean("defaultValidator"); assertThat(((ValidatorAdapter) validator).getTarget()) .isSameAs(defaultValidator); // Primary Spring validator is the one used by MVC behind the scenes - assertThat(loaded.getBean(Validator.class)) + assertThat(context.getBean(Validator.class)) .isEqualTo(defaultValidator); }); } @Test public void validatorWithCustomJsr303ValidatorExposedAsSpringValidator() { - this.context + this.contextRunner .withConfiguration( AutoConfigurations.of(ValidationAutoConfiguration.class)) - .withUserConfiguration(CustomJsr303Validator.class).run((loaded) -> { - assertThat(loaded).doesNotHaveBean(ValidatorFactory.class); - assertThat(loaded).getBeanNames(javax.validation.Validator.class) + .withUserConfiguration(CustomJsr303Validator.class).run((context) -> { + assertThat(context).doesNotHaveBean(ValidatorFactory.class); + assertThat(context).getBeanNames(javax.validation.Validator.class) .containsOnly("customJsr303Validator"); - assertThat(loaded).getBeanNames(Validator.class) + assertThat(context).getBeanNames(Validator.class) .containsOnly("mvcValidator"); - Validator validator = loaded.getBean(Validator.class); + Validator validator = context.getBean(Validator.class); assertThat(validator).isInstanceOf(ValidatorAdapter.class); Validator target = ((ValidatorAdapter) validator).getTarget(); assertThat(ReflectionTestUtils.getField(target, "targetValidator")) - .isSameAs(loaded.getBean("customJsr303Validator")); + .isSameAs(context.getBean("customJsr303Validator")); }); } @Test public void httpMessageConverterThatUsesConversionServiceDoesNotCreateACycle() { - this.context.withUserConfiguration(CustomHttpMessageConverter.class) - .run((loaded) -> assertThat(loaded).hasNotFailed()); + this.contextRunner.withUserConfiguration(CustomHttpMessageConverter.class) + .run((context) -> assertThat(context).hasNotFailed()); } protected Map> getFaviconMappingLocations( diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java index 50356f5fcf..3499665328 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java @@ -24,7 +24,7 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.WebApplicationContextTester; +import org.springframework.boot.test.context.WebApplicationContextRunner; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.test.util.ReflectionTestUtils; @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class WebServicesAutoConfigurationTests { - private final WebApplicationContextTester context = new WebApplicationContextTester() + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(WebServicesAutoConfiguration.class)); @Rule @@ -48,15 +48,15 @@ public class WebServicesAutoConfigurationTests { @Test public void defaultConfiguration() { - this.context.run((loaded) -> assertThat(loaded) + this.contextRunner.run((context) -> assertThat(context) .hasSingleBean(ServletRegistrationBean.class)); } @Test public void customPathMustBeginWithASlash() { - this.context.withPropertyValues("spring.webservices.path=invalid") - .run((loaded) -> { - assertThat(loaded).getFailure() + this.contextRunner.withPropertyValues("spring.webservices.path=invalid") + .run((context) -> { + assertThat(context).getFailure() .isInstanceOf(BeanCreationException.class) .hasMessageContaining( "Failed to bind properties under 'spring.webservices'"); @@ -65,21 +65,22 @@ public class WebServicesAutoConfigurationTests { @Test public void customPath() { - this.context.withPropertyValues("spring.webservices.path=/valid") - .run((loaded) -> assertThat(getUrlMappings(loaded)).contains("/valid/*")); + this.contextRunner.withPropertyValues("spring.webservices.path=/valid").run( + (context) -> assertThat(getUrlMappings(context)).contains("/valid/*")); } @Test public void customPathWithTrailingSlash() { - this.context.withPropertyValues("spring.webservices.path=/valid/") - .run((loaded) -> assertThat(getUrlMappings(loaded)).contains("/valid/*")); + this.contextRunner.withPropertyValues("spring.webservices.path=/valid/").run( + (context) -> assertThat(getUrlMappings(context)).contains("/valid/*")); } @Test public void customLoadOnStartup() { - this.context.withPropertyValues("spring.webservices.servlet.load-on-startup=1") - .run((loaded) -> { - ServletRegistrationBean registrationBean = loaded + this.contextRunner + .withPropertyValues("spring.webservices.servlet.load-on-startup=1") + .run((context) -> { + ServletRegistrationBean registrationBean = context .getBean(ServletRegistrationBean.class); assertThat(ReflectionTestUtils.getField(registrationBean, "loadOnStartup")).isEqualTo(1); @@ -88,17 +89,17 @@ public class WebServicesAutoConfigurationTests { @Test public void customInitParameters() { - this.context + this.contextRunner .withPropertyValues("spring.webservices.servlet.init.key1=value1", "spring.webservices.servlet.init.key2=value2") - .run(loaded -> assertThat( - getServletRegistrationBean(loaded).getInitParameters()) + .run((context) -> assertThat( + getServletRegistrationBean(context).getInitParameters()) .containsEntry("key1", "value1") .containsEntry("key2", "value2")); } - private Collection getUrlMappings(ApplicationContext loaded) { - return getServletRegistrationBean(loaded).getUrlMappings(); + private Collection getUrlMappings(ApplicationContext context) { + return getServletRegistrationBean(context).getUrlMappings(); } private ServletRegistrationBean getServletRegistrationBean( diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java index d60ad2c24e..25562014ca 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java @@ -21,7 +21,7 @@ import javax.sql.DataSource; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; @@ -38,24 +38,24 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class TestDatabaseAutoConfigurationTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration( AutoConfigurations.of(TestDatabaseAutoConfiguration.class)); @Test public void replaceWithNoDataSourceAvailable() { - this.context - .run((loaded) -> assertThat(loaded).doesNotHaveBean(DataSource.class)); + this.contextRunner + .run((context) -> assertThat(context).doesNotHaveBean(DataSource.class)); } @Test public void replaceWithUniqueDatabase() { - this.context.withUserConfiguration(ExistingDataSourceConfiguration.class) - .run((loaded) -> { - DataSource datasource = loaded.getBean(DataSource.class); + this.contextRunner.withUserConfiguration(ExistingDataSourceConfiguration.class) + .run((context) -> { + DataSource datasource = context.getBean(DataSource.class); JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource); jdbcTemplate.execute("create table example (id int, name varchar);"); - this.context.run((secondContext) -> { + this.contextRunner.run((secondContext) -> { DataSource anotherDatasource = secondContext .getBean(DataSource.class); JdbcTemplate anotherJdbcTemplate = new JdbcTemplate( diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java index 0107e376c9..2f4f9c3817 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java @@ -24,7 +24,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextTester; +import org.springframework.boot.test.context.ApplicationContextRunner; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.context.annotation.Bean; @@ -44,15 +44,15 @@ import static org.mockito.Mockito.mock; @ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" }) public class TestDatabaseAutoConfigurationNoEmbeddedTests { - private final ApplicationContextTester context = new ApplicationContextTester() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withUserConfiguration(ExistingDataSourceConfiguration.class) .withConfiguration( AutoConfigurations.of(TestDatabaseAutoConfiguration.class)); @Test public void applyAnyReplace() { - this.context.run((loaded) -> { - assertThat(loaded).getFailure().isInstanceOf(BeanCreationException.class) + this.contextRunner.run((context) -> { + assertThat(context).getFailure().isInstanceOf(BeanCreationException.class) .hasMessageContaining( "Failed to replace DataSource with an embedded database for tests.") .hasMessageContaining( @@ -64,11 +64,11 @@ public class TestDatabaseAutoConfigurationNoEmbeddedTests { @Test public void applyNoReplace() { - this.context.withPropertyValues("spring.test.database.replace=NONE") - .run((loaded) -> { - assertThat(loaded).hasSingleBean(DataSource.class); - assertThat(loaded).getBean(DataSource.class) - .isSameAs(loaded.getBean("myCustomDataSource")); + this.contextRunner.withPropertyValues("spring.test.database.replace=NONE") + .run((context) -> { + assertThat(context).hasSingleBean(DataSource.class); + assertThat(context).getBean(DataSource.class) + .isSameAs(context.getBean("myCustomDataSource")); }); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java similarity index 90% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextTester.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java index 669aaff44f..f2423bbdcb 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextTester.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java @@ -34,13 +34,13 @@ import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** - * Tester utility design to manage the lifecycle of an {@link ApplicationContext} and - * provide AssertJ style assertions. The test is best used as a field of a test class, - * describing the shared configuration required for the test: + * Utility design to run and an {@link ApplicationContext} and provide AssertJ style + * assertions. The test is best used as a field of a test class, describing the shared + * configuration required for the test: * *
  * public class MyContextTests {
- *     private final ApplicationContextTester context = new ApplicationContextTester()
+ *     private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
  *             .withPropertyValues("spring.foo=bar")
  *             .withUserConfiguration(MyConfiguration.class);
  * }
@@ -55,8 +55,8 @@ import org.springframework.util.ReflectionUtils; *
  * @Test
  * public someTest() {
- *     this.context.withPropertyValues("spring.foo=biz").run((loaded) -> {
- *         assertThat(loaded).containsSingleBean(MyBean.class);
+ *     this.contextRunner.withPropertyValues("spring.foo=biz").run((context) -> {
+ *         assertThat(context).containsSingleBean(MyBean.class);
  *         // other assertions
  *     });
  * }
@@ -80,19 +80,19 @@ import org.springframework.util.ReflectionUtils; * } *

* - * @param The "self" type for this tester + * @param The "self" type for this runner * @param The context type * @param The application context assertion provider * @author Stephane Nicoll * @author Andy Wilkinson * @author Phillip Webb * @since 2.0.0 - * @see ApplicationContextTester - * @see WebApplicationContextTester - * @see ReactiveWebApplicationContextTester + * @see ApplicationContextRunner + * @see WebApplicationContextRunner + * @see ReactiveWebApplicationContextRunner * @see ApplicationContextAssert */ -abstract class AbstractApplicationContextTester, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { +abstract class AbstractApplicationContextRunner, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { private final Supplier contextFactory; @@ -107,10 +107,10 @@ abstract class AbstractApplicationContextTester configurations = new ArrayList<>(); /** - * Create a new {@link AbstractApplicationContextTester} instance. + * Create a new {@link AbstractApplicationContextRunner} instance. * @param contextFactory the factory used to create the actual context */ - protected AbstractApplicationContextTester(Supplier contextFactory) { + protected AbstractApplicationContextRunner(Supplier contextFactory) { Assert.notNull(contextFactory, "ContextFactory must not be null"); this.contextFactory = contextFactory; this.environmentProperties = TestPropertyValues.empty(); @@ -243,7 +243,7 @@ abstract class AbstractApplicationContextTester assertType = (Class) resolvableType.resolveGeneric(1); Class contextType = (Class) resolvableType.resolveGeneric(2); return AssertProviderApplicationContext.get(assertType, contextType, diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java index 364cfbb4fb..bc98986d13 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java @@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @param The application context type * @author Phillip Webb * @since 2.0.0 - * @see ApplicationContextTester + * @see ApplicationContextRunner * @see AssertableApplicationContext */ public class ApplicationContextAssert diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java similarity index 76% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextTester.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java index f133c27d84..42650c1aad 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextTester.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java @@ -22,33 +22,33 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** - * A {@link AbstractApplicationContextTester ApplicationContext tester} for a standard, + * A {@link AbstractApplicationContextRunner ApplicationContext runner} for a standard, * non-web environment {@link ConfigurableApplicationContext}. *

- * See {@link AbstractApplicationContextTester} for details. + * See {@link AbstractApplicationContextRunner} for details. * * @author Stephane Nicoll * @author Andy Wilkinson * @author Phillip Webb * @since 2.0.0 */ -public class ApplicationContextTester extends - AbstractApplicationContextTester { +public class ApplicationContextRunner extends + AbstractApplicationContextRunner { /** - * Create a new {@link ApplicationContextTester} instance using an + * Create a new {@link ApplicationContextRunner} instance using an * {@link AnnotationConfigApplicationContext} as the underlying source. */ - public ApplicationContextTester() { + public ApplicationContextRunner() { this(AnnotationConfigApplicationContext::new); } /** - * Create a new {@link ApplicationContextTester} instance using the specified + * Create a new {@link ApplicationContextRunner} instance using the specified * {@code contextFactory} as the underlying source. * @param contextFactory a supplier that returns a new instance on each call */ - public ApplicationContextTester( + public ApplicationContextRunner( Supplier contextFactory) { super(contextFactory); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java index 8ac8fe82cc..195fb222f2 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java @@ -30,7 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext; * * @author Phillip Webb * @since 2.0.0 - * @see ApplicationContextTester + * @see ApplicationContextRunner * @see ApplicationContext */ public interface AssertableApplicationContext diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java index a5815c332c..b511496e4a 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java @@ -30,7 +30,7 @@ import org.springframework.web.context.WebApplicationContext; * * @author Phillip Webb * @since 2.0.0 - * @see WebApplicationContextTester + * @see WebApplicationContextRunner * @see WebApplicationContext */ public interface AssertableWebApplicationContext diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java index f4523d9748..18052939ed 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java @@ -26,7 +26,7 @@ import org.springframework.context.ApplicationContext; * @author Andy Wilkinson * @param The application context type * @since 2.0.0 - * @see AbstractApplicationContextTester + * @see AbstractApplicationContextRunner */ @FunctionalInterface public interface ContextConsumer { diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java similarity index 73% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTester.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java index 3044d6bf6b..1849ff7859 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTester.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java @@ -22,33 +22,33 @@ import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebAppl import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; /** - * A {@link AbstractApplicationContextTester ApplicationContext tester} for a + * A {@link AbstractApplicationContextRunner ApplicationContext runner} for a * {@link ConfigurableReactiveWebApplicationContext}. *

- * See {@link AbstractApplicationContextTester} for details. + * See {@link AbstractApplicationContextRunner} for details. * * @author Andy Wilkinson * @author Stephane Nicoll * @author Phillip Webb * @since 2.0.0 */ -public final class ReactiveWebApplicationContextTester extends - AbstractApplicationContextTester { +public final class ReactiveWebApplicationContextRunner extends + AbstractApplicationContextRunner { /** - * Create a new {@link ReactiveWebApplicationContextTester} instance using a + * Create a new {@link ReactiveWebApplicationContextRunner} instance using a * {@link GenericReactiveWebApplicationContext} as the underlying source. */ - public ReactiveWebApplicationContextTester() { + public ReactiveWebApplicationContextRunner() { this(GenericReactiveWebApplicationContext::new); } /** - * Create a new {@link ApplicationContextTester} instance using the specified + * Create a new {@link ApplicationContextRunner} instance using the specified * {@code contextFactory} as the underlying source. * @param contextFactory a supplier that returns a new instance on each call */ - public ReactiveWebApplicationContextTester( + public ReactiveWebApplicationContextRunner( Supplier contextFactory) { super(contextFactory); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextTester.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java similarity index 83% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextTester.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java index 98101f6e8b..9645df427f 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextTester.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java @@ -24,35 +24,35 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; /** - * A {@link AbstractApplicationContextTester ApplicationContext tester} for a Servlet + * A {@link AbstractApplicationContextRunner ApplicationContext runner} for a Servlet * based {@link ConfigurableWebApplicationContext}. *

- * See {@link AbstractApplicationContextTester} for details. + * See {@link AbstractApplicationContextRunner} for details. * * @author Andy Wilkinson * @author Stephane Nicoll * @author Phillip Webb * @since 2.0.0 */ -public final class WebApplicationContextTester extends - AbstractApplicationContextTester { +public final class WebApplicationContextRunner extends + AbstractApplicationContextRunner { /** - * Create a new {@link WebApplicationContextTester} instance using an + * Create a new {@link WebApplicationContextRunner} instance using an * {@link AnnotationConfigWebApplicationContext} with a {@link MockServletContext} as * the underlying source. * @see #withMockServletContext(Supplier) */ - public WebApplicationContextTester() { + public WebApplicationContextRunner() { this(withMockServletContext(AnnotationConfigWebApplicationContext::new)); } /** - * Create a new {@link WebApplicationContextTester} instance using the specified + * Create a new {@link WebApplicationContextRunner} instance using the specified * {@code contextFactory} as the underlying source. * @param contextFactory a supplier that returns a new instance on each call */ - public WebApplicationContextTester( + public WebApplicationContextRunner( Supplier contextFactory) { super(contextFactory); } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java similarity index 96% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextTesterTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java index 5463d2b304..73fa1508aa 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextTesterTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java @@ -34,15 +34,15 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; /** - * Abstract tests for {@link AbstractApplicationContextTester} implementations. + * Abstract tests for {@link AbstractApplicationContextRunner} implementations. * - * @param The tester type + * @param The runner type * @param the context type * @param the assertable context type * @author Stephane Nicoll * @author Phillip Webb */ -public abstract class AbstractApplicationContextTesterTests, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { +public abstract class AbstractApplicationContextRunnerTests, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { @Rule public final ExpectedException thrown = ExpectedException.none(); diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java similarity index 78% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextTesterTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java index 01c5f1e3b3..408abf753f 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextTesterTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java @@ -19,17 +19,17 @@ package org.springframework.boot.test.context; import org.springframework.context.ConfigurableApplicationContext; /** - * Tests for {@link ApplicationContextTester}. + * Tests for {@link ApplicationContextRunner}. * * @author Stephane Nicoll * @author Phillip Webb */ -public class ApplicationContextTesterTests extends - AbstractApplicationContextTesterTests { +public class ApplicationContextRunnerTests extends + AbstractApplicationContextRunnerTests { @Override - protected ApplicationContextTester get() { - return new ApplicationContextTester(); + protected ApplicationContextRunner get() { + return new ApplicationContextRunner(); } } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java similarity index 71% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTesterTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java index 6a7d7753f4..7ddf7ee134 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextTesterTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java @@ -19,17 +19,17 @@ package org.springframework.boot.test.context; import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext; /** - * Tests for {@link ReactiveWebApplicationContextTester}. + * Tests for {@link ReactiveWebApplicationContextRunner}. * * @author Stephane Nicoll * @author Phillip Webb */ -public class ReactiveWebApplicationContextTesterTests extends - AbstractApplicationContextTesterTests { +public class ReactiveWebApplicationContextRunnerTests extends + AbstractApplicationContextRunnerTests { @Override - protected ReactiveWebApplicationContextTester get() { - return new ReactiveWebApplicationContextTester(); + protected ReactiveWebApplicationContextRunner get() { + return new ReactiveWebApplicationContextRunner(); } } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextTesterTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java similarity index 78% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextTesterTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java index 4b37ec6345..065306fc9b 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextTesterTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java @@ -24,13 +24,13 @@ import org.springframework.web.context.ConfigurableWebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link WebApplicationContextTester}. + * Tests for {@link WebApplicationContextRunner}. * * @author Stephane Nicoll * @author Phillip Webb */ -public class WebApplicationContextTesterTests extends - AbstractApplicationContextTesterTests { +public class WebApplicationContextRunnerTests extends + AbstractApplicationContextRunnerTests { @Test public void contextShouldHaveMockServletContext() throws Exception { @@ -39,8 +39,8 @@ public class WebApplicationContextTesterTests extends } @Override - protected WebApplicationContextTester get() { - return new WebApplicationContextTester(); + protected WebApplicationContextRunner get() { + return new WebApplicationContextRunner(); } } From 07556cda5181f2705cf3fb7a5aae3c5d98bb1a1a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 26 Jul 2017 13:49:54 -0700 Subject: [PATCH 2/4] Restructure `boot.test.context` package Split up `org.springframework.boot.test.context` into distinct packages for `runner` and `assertj`. See gh-9875 --- ...HealthIndicatorAutoConfigurationTests.java | 6 +-- .../cache/CacheAutoConfigurationTests.java | 6 +-- .../Neo4jDataAutoConfigurationTests.java | 2 +- ...HazelcastAutoConfigurationClientTests.java | 2 +- ...HazelcastAutoConfigurationServerTests.java | 2 +- .../HazelcastAutoConfigurationTests.java | 2 +- .../InfluxDbAutoConfigurationTests.java | 4 +- .../DataSourceAutoConfigurationTests.java | 4 +- .../jms/JmsAutoConfigurationTests.java | 4 +- .../ActiveMQAutoConfigurationTests.java | 2 +- .../ArtemisAutoConfigurationTests.java | 2 +- .../HttpHandlerAutoConfigurationTests.java | 2 +- .../servlet/WebMvcAutoConfigurationTests.java | 6 +-- .../WebServicesAutoConfigurationTests.java | 2 +- .../TestDatabaseAutoConfigurationTests.java | 2 +- ...abaseAutoConfigurationNoEmbeddedTests.java | 2 +- .../ApplicationContextAssert.java | 5 +- .../ApplicationContextAssertProvider.java} | 12 ++--- ...erApplicationContextInvocationHandler.java | 4 +- .../AssertableApplicationContext.java | 9 ++-- ...sertableReactiveWebApplicationContext.java | 8 ++-- .../AssertableWebApplicationContext.java | 9 ++-- .../test/context/assertj/package-info.java | 20 ++++++++ .../AbstractApplicationContextRunner.java | 9 ++-- .../ApplicationContextRunner.java | 3 +- .../context/{ => runner}/ContextConsumer.java | 2 +- .../ReactiveWebApplicationContextRunner.java | 3 +- .../WebApplicationContextRunner.java | 3 +- .../test/context/runner/package-info.java | 20 ++++++++ ...pplicationContextAssertProviderTests.java} | 48 +++++++++---------- .../ApplicationContextAssertTests.java | 2 +- .../AssertableApplicationContextTests.java | 4 +- ...bleReactiveWebApplicationContextTests.java | 4 +- .../AssertableWebApplicationContextTests.java | 4 +- ...AbstractApplicationContextRunnerTests.java | 8 ++-- .../ApplicationContextRunnerTests.java | 3 +- ...ctiveWebApplicationContextRunnerTests.java | 3 +- .../WebApplicationContextRunnerTests.java | 3 +- 38 files changed, 145 insertions(+), 91 deletions(-) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => assertj}/ApplicationContextAssert.java (98%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{AssertProviderApplicationContext.java => assertj/ApplicationContextAssertProvider.java} (90%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => assertj}/AssertProviderApplicationContextInvocationHandler.java (97%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => assertj}/AssertableApplicationContext.java (83%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => assertj}/AssertableReactiveWebApplicationContext.java (89%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => assertj}/AssertableWebApplicationContext.java (84%) create mode 100644 spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/package-info.java rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => runner}/AbstractApplicationContextRunner.java (96%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => runner}/ApplicationContextRunner.java (93%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => runner}/ContextConsumer.java (95%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => runner}/ReactiveWebApplicationContextRunner.java (93%) rename spring-boot-test/src/main/java/org/springframework/boot/test/context/{ => runner}/WebApplicationContextRunner.java (95%) create mode 100644 spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/package-info.java rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{AssertProviderApplicationContextTests.java => assertj/ApplicationContextAssertProviderTests.java} (82%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => assertj}/ApplicationContextAssertTests.java (99%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => assertj}/AssertableApplicationContextTests.java (92%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => assertj}/AssertableReactiveWebApplicationContextTests.java (92%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => assertj}/AssertableWebApplicationContextTests.java (92%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => runner}/AbstractApplicationContextRunnerTests.java (94%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => runner}/ApplicationContextRunnerTests.java (88%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => runner}/ReactiveWebApplicationContextRunnerTests.java (88%) rename spring-boot-test/src/test/java/org/springframework/boot/test/context/{ => runner}/WebApplicationContextRunnerTests.java (91%) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java index 79683c81dd..3e91dc5fda 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java @@ -60,9 +60,9 @@ import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.context.ApplicationContextRunner; -import org.springframework.boot.test.context.AssertableApplicationContext; -import org.springframework.boot.test.context.ContextConsumer; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.core.CassandraOperations; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 44fba0a485..459b92a6b9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -54,9 +54,9 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextRunner; -import org.springframework.boot.test.context.AssertableApplicationContext; -import org.springframework.boot.test.context.ContextConsumer; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.cache.Cache; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java index 47c64753cc..16f94b4eac 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfigurationTests.java @@ -29,7 +29,7 @@ import org.springframework.boot.autoconfigure.data.neo4j.city.City; import org.springframework.boot.autoconfigure.data.neo4j.country.Country; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration; -import org.springframework.boot.test.context.WebApplicationContextRunner; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index d5f49f28ac..9d0b62f091 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -30,7 +30,7 @@ import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index c6714e5190..6ed7efea40 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java @@ -28,7 +28,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextRunner; +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.Bean; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java index c296b73569..d3309a05d1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java @@ -23,7 +23,7 @@ import com.hazelcast.core.HazelcastInstance; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.core.io.ClassPathResource; import static org.assertj.core.api.Assertions.assertThat; 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 cc102d93d8..cc9481ca78 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 @@ -25,8 +25,8 @@ import retrofit2.Retrofit; import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextRunner; -import org.springframework.boot.test.context.AssertableApplicationContext; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index 8b0e5a4188..980701425f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -39,9 +39,9 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.jdbc.DatabaseDriver; -import org.springframework.boot.test.context.ApplicationContextRunner; -import org.springframework.boot.test.context.AssertableApplicationContext; import org.springframework.boot.test.context.HidePackagesClassLoader; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index b23769eb99..4b21f58ed5 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -28,8 +28,8 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextRunner; -import org.springframework.boot.test.context.AssertableApplicationContext; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +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.Primary; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java index 4c3a492f2d..18f2e03a56 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index edab68342c..bcd71ba404 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -42,7 +42,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; -import org.springframework.boot.test.context.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; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java index 850d81570c..b00998a840 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/HttpHandlerAutoConfigurationTests.java @@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.web.reactive; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ReactiveWebApplicationContextRunner; +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.server.reactive.HttpHandler; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 1bc41ca63c..6fa98743ea 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -38,9 +38,9 @@ import org.springframework.boot.autoconfigure.validation.ValidationAutoConfigura import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WelcomePageHandlerMapping; -import org.springframework.boot.test.context.AssertableWebApplicationContext; -import org.springframework.boot.test.context.ContextConsumer; -import org.springframework.boot.test.context.WebApplicationContextRunner; +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; +import org.springframework.boot.test.context.runner.ContextConsumer; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; import org.springframework.boot.web.servlet.filter.OrderedHttpPutFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java index 3499665328..6eb93e150e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfigurationTests.java @@ -24,7 +24,7 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.WebApplicationContextRunner; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.test.util.ReflectionTestUtils; diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java index 25562014ca..b688c00c64 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java @@ -21,7 +21,7 @@ import javax.sql.DataSource; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java index 2f4f9c3817..fccbb37068 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationNoEmbeddedTests.java @@ -24,7 +24,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration; -import org.springframework.boot.test.context.ApplicationContextRunner; +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.Bean; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java similarity index 98% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java index bc98986d13..35bf1d30d4 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextAssert.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractObjectArrayAssert; @@ -25,13 +25,14 @@ import org.assertj.core.api.MapAssert; import org.assertj.core.error.BasicErrorMessageFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.ApplicationContext; import org.springframework.util.Assert; import static org.assertj.core.api.Assertions.assertThat; /** - * AspectJ {@link org.assertj.core.api.Assert assertions} that can be applied to an + * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to an * {@link ApplicationContext}. * * @param The application context type diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java similarity index 90% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContext.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java index 7ea2860d7b..00c267bc88 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.io.Closeable; import java.lang.reflect.Proxy; @@ -48,7 +48,7 @@ import org.springframework.util.Assert; * @see AssertableReactiveWebApplicationContext * @see ApplicationContextAssert */ -interface AssertProviderApplicationContext extends +public interface ApplicationContextAssertProvider extends ApplicationContext, AssertProvider>, Closeable { /** @@ -88,19 +88,19 @@ interface AssertProviderApplicationContext extends void close(); /** - * Factory method to create a new {@link AssertProviderApplicationContext} instance. + * Factory method to create a new {@link ApplicationContextAssertProvider} instance. * @param the assert provider type * @param the context type - * @param type the type of {@link AssertProviderApplicationContext} required (must be + * @param type the type of {@link ApplicationContextAssertProvider} required (must be * an interface) * @param contextType the type of {@link ApplicationContext} being managed (must be an * interface) * @param contextSupplier a supplier that will either return a fully configured * {@link ApplicationContext} or throw an exception if the context fails to start. - * @return a {@link AssertProviderApplicationContext} instance + * @return a {@link ApplicationContextAssertProvider} instance */ @SuppressWarnings("unchecked") - static , C extends ApplicationContext> T get( + static , C extends ApplicationContext> T get( Class type, Class contextType, Supplier contextSupplier) { Assert.notNull(type, "Type must not be null"); diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContextInvocationHandler.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertProviderApplicationContextInvocationHandler.java similarity index 97% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContextInvocationHandler.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertProviderApplicationContextInvocationHandler.java index 0519c7de30..67948df423 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertProviderApplicationContextInvocationHandler.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertProviderApplicationContextInvocationHandler.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.io.Closeable; import java.io.IOException; @@ -31,7 +31,7 @@ import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; /** - * {@link InvocationHandler} used by {@link AssertProviderApplicationContext} generated + * {@link InvocationHandler} used by {@link ApplicationContextAssertProvider} generated * proxies. * * @author Phillip Webb diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java similarity index 83% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java index 195fb222f2..9195ad50b9 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.util.function.Supplier; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -26,7 +27,7 @@ import org.springframework.context.ConfigurableApplicationContext; * be used to decorate and existing application context or an application context that * failed to start. *

- * See {@link AssertProviderApplicationContext} for more details. + * See {@link ApplicationContextAssertProvider} for more details. * * @author Phillip Webb * @since 2.0.0 @@ -34,7 +35,7 @@ import org.springframework.context.ConfigurableApplicationContext; * @see ApplicationContext */ public interface AssertableApplicationContext - extends AssertProviderApplicationContext { + extends ApplicationContextAssertProvider { /** * Factory method to create a new {@link AssertableApplicationContext} instance. @@ -45,7 +46,7 @@ public interface AssertableApplicationContext */ static AssertableApplicationContext get( Supplier contextSupplier) { - return AssertProviderApplicationContext.get(AssertableApplicationContext.class, + return ApplicationContextAssertProvider.get(AssertableApplicationContext.class, ConfigurableApplicationContext.class, contextSupplier); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java similarity index 89% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContext.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java index 86e5703d4f..418cff4301 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.util.function.Supplier; @@ -26,7 +26,7 @@ import org.springframework.boot.web.reactive.context.ReactiveWebApplicationConte * assertions. Can be used to decorate and existing reactive web application context or an * application context that failed to start. *

- * See {@link AssertProviderApplicationContext} for more details. + * See {@link ApplicationContextAssertProvider} for more details. * * @author Phillip Webb * @since 2.0.0 @@ -34,7 +34,7 @@ import org.springframework.boot.web.reactive.context.ReactiveWebApplicationConte * @see ReactiveWebApplicationContext */ public interface AssertableReactiveWebApplicationContext extends - AssertProviderApplicationContext, + ApplicationContextAssertProvider, ReactiveWebApplicationContext { /** @@ -47,7 +47,7 @@ public interface AssertableReactiveWebApplicationContext extends */ static AssertableReactiveWebApplicationContext get( Supplier contextSupplier) { - return AssertProviderApplicationContext.get( + return ApplicationContextAssertProvider.get( AssertableReactiveWebApplicationContext.class, ConfigurableReactiveWebApplicationContext.class, contextSupplier); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java similarity index 84% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java index b511496e4a..9778420f48 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AssertableWebApplicationContext.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.util.function.Supplier; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; @@ -26,7 +27,7 @@ import org.springframework.web.context.WebApplicationContext; * Can be used to decorate and existing servlet web application context or an application * context that failed to start. *

- * See {@link AssertProviderApplicationContext} for more details. + * See {@link ApplicationContextAssertProvider} for more details. * * @author Phillip Webb * @since 2.0.0 @@ -34,7 +35,7 @@ import org.springframework.web.context.WebApplicationContext; * @see WebApplicationContext */ public interface AssertableWebApplicationContext - extends AssertProviderApplicationContext, + extends ApplicationContextAssertProvider, WebApplicationContext { /** @@ -46,7 +47,7 @@ public interface AssertableWebApplicationContext */ static AssertableWebApplicationContext get( Supplier contextSupplier) { - return AssertProviderApplicationContext.get(AssertableWebApplicationContext.class, + return ApplicationContextAssertProvider.get(AssertableWebApplicationContext.class, ConfigurableWebApplicationContext.class, contextSupplier); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/package-info.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/package-info.java new file mode 100644 index 0000000000..df4996cf1c --- /dev/null +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * AssertJ support for ApplicationContexts. + */ +package org.springframework.boot.test.context.assertj; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java similarity index 96% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index f2423bbdcb..270beff416 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/AbstractApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import java.util.ArrayList; import java.util.Arrays; @@ -23,6 +23,9 @@ import java.util.function.Supplier; import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.context.annotation.UserConfigurations; +import org.springframework.boot.test.context.HidePackagesClassLoader; +import org.springframework.boot.test.context.assertj.ApplicationContextAssert; +import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -92,7 +95,7 @@ import org.springframework.util.ReflectionUtils; * @see ReactiveWebApplicationContextRunner * @see ApplicationContextAssert */ -abstract class AbstractApplicationContextRunner, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { +abstract class AbstractApplicationContextRunner, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { private final Supplier contextFactory; @@ -246,7 +249,7 @@ abstract class AbstractApplicationContextRunner assertType = (Class) resolvableType.resolveGeneric(1); Class contextType = (Class) resolvableType.resolveGeneric(2); - return AssertProviderApplicationContext.get(assertType, contextType, + return ApplicationContextAssertProvider.get(assertType, contextType, this::createAndLoadContext); } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java similarity index 93% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java index 42650c1aad..d3799554bd 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import java.util.function.Supplier; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ContextConsumer.java similarity index 95% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ContextConsumer.java index 18052939ed..e6da8ca150 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextConsumer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ContextConsumer.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import org.springframework.context.ApplicationContext; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java similarity index 93% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java index 1849ff7859..07ec6fc2fd 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import java.util.function.Supplier; +import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java similarity index 95% rename from spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java rename to spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java index 9645df427f..ea3e9b1c54 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/WebApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import java.util.function.Supplier; +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/package-info.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/package-info.java new file mode 100644 index 0000000000..bd8554f0fd --- /dev/null +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Test utilities to run application contexts for testing. + */ +package org.springframework.boot.test.context.runner; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertProviderApplicationContextTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java similarity index 82% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertProviderApplicationContextTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java index 3d7d8549ed..e88ff857da 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertProviderApplicationContextTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import java.util.function.Supplier; @@ -34,12 +34,12 @@ import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.verify; /** - * Tests for {@link AssertProviderApplicationContext} and + * Tests for {@link ApplicationContextAssertProvider} and * {@link AssertProviderApplicationContextInvocationHandler}. * * @author Phillip Webb */ -public class AssertProviderApplicationContextTests { +public class ApplicationContextAssertProviderTests { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -67,7 +67,7 @@ public class AssertProviderApplicationContextTests { public void getWhenTypeIsNullShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Type must not be null"); - AssertProviderApplicationContext.get(null, ApplicationContext.class, + ApplicationContextAssertProvider.get(null, ApplicationContext.class, this.mockContextSupplier); } @@ -75,7 +75,7 @@ public class AssertProviderApplicationContextTests { public void getWhenTypeIsClassShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Type must not be null"); - AssertProviderApplicationContext.get(null, ApplicationContext.class, + ApplicationContextAssertProvider.get(null, ApplicationContext.class, this.mockContextSupplier); } @@ -83,7 +83,7 @@ public class AssertProviderApplicationContextTests { public void getWhenContextTypeIsNullShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Type must be an interface"); - AssertProviderApplicationContext.get( + ApplicationContextAssertProvider.get( TestAssertProviderApplicationContextClass.class, ApplicationContext.class, this.mockContextSupplier); } @@ -92,7 +92,7 @@ public class AssertProviderApplicationContextTests { public void getWhenContextTypeIsClassShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("ContextType must not be null"); - AssertProviderApplicationContext.get(TestAssertProviderApplicationContext.class, + ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class, null, this.mockContextSupplier); } @@ -100,14 +100,14 @@ public class AssertProviderApplicationContextTests { public void getWhenSupplierIsNullShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("ContextType must be an interface"); - AssertProviderApplicationContext.get(TestAssertProviderApplicationContext.class, + ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class, StaticApplicationContext.class, this.mockContextSupplier); } @Test public void getWhenContextStartsShouldReturnProxyThatCallsRealMethods() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); assertThat((Object) context).isNotNull(); context.getBean("foo"); @@ -117,7 +117,7 @@ public class AssertProviderApplicationContextTests { @Test public void getWhenContextFailsShouldReturnProxyThatThrowsExceptions() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); assertThat((Object) context).isNotNull(); expectStartupFailure(); @@ -127,14 +127,14 @@ public class AssertProviderApplicationContextTests { @Test public void getSourceContextWhenContextStartsShouldReturnSourceContext() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); assertThat(context.getSourceApplicationContext()).isSameAs(this.mockContext); } @Test public void getSourceContextWhenContextFailsShouldThrowException() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); expectStartupFailure(); context.getSourceApplicationContext(); @@ -143,7 +143,7 @@ public class AssertProviderApplicationContextTests { @Test public void getSourceContextOfTypeWhenContextStartsShouldReturnSourceContext() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); assertThat(context.getSourceApplicationContext(ApplicationContext.class)) .isSameAs(this.mockContext); @@ -152,7 +152,7 @@ public class AssertProviderApplicationContextTests { @Test public void getSourceContextOfTypeWhenContextFailsToStartShouldThrowException() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); expectStartupFailure(); context.getSourceApplicationContext(ApplicationContext.class); @@ -160,7 +160,7 @@ public class AssertProviderApplicationContextTests { @Test public void getStartupFailureWhenContextStartsShouldReturnNull() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); assertThat(context.getStartupFailure()).isNull(); } @@ -168,14 +168,14 @@ public class AssertProviderApplicationContextTests { @Test public void getStartupFailureWhenContextFailsToStartShouldReturnException() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); assertThat(context.getStartupFailure()).isEqualTo(this.startupFailure); } @Test public void assertThatWhenContextStartsShouldReturnAssertions() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); ApplicationContextAssert contextAssert = assertThat(context); assertThat(contextAssert.getApplicationContext()).isSameAs(context); @@ -184,7 +184,7 @@ public class AssertProviderApplicationContextTests { @Test public void assertThatWhenContextFailsShouldReturnAssertions() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); ApplicationContextAssert contextAssert = assertThat(context); assertThat(contextAssert.getApplicationContext()).isSameAs(context); @@ -193,7 +193,7 @@ public class AssertProviderApplicationContextTests { @Test public void toStringWhenContextStartsShouldReturnSimpleString() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); assertThat(context.toString()) .startsWith( @@ -204,7 +204,7 @@ public class AssertProviderApplicationContextTests { @Test public void toStringWhenContextFailsToStartShouldReturnSimpleString() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.startupFailureSupplier); assertThat(context.toString()).isEqualTo("Unstarted application context " + "org.springframework.context.ApplicationContext" @@ -213,7 +213,7 @@ public class AssertProviderApplicationContextTests { @Test public void closeShouldCloseContext() throws Exception { - AssertProviderApplicationContext context = get( + ApplicationContextAssertProvider context = get( this.mockContextSupplier); context.close(); verify(this.mockContext).close(); @@ -225,15 +225,15 @@ public class AssertProviderApplicationContextTests { this.thrown.expectCause(equalTo(this.startupFailure)); } - private AssertProviderApplicationContext get( + private ApplicationContextAssertProvider get( Supplier contextSupplier) { - return AssertProviderApplicationContext.get( + return ApplicationContextAssertProvider.get( TestAssertProviderApplicationContext.class, ApplicationContext.class, contextSupplier); } private interface TestAssertProviderApplicationContext - extends AssertProviderApplicationContext { + extends ApplicationContextAssertProvider { } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextAssertTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertTests.java similarity index 99% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextAssertTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertTests.java index 798286612b..b9767bbd06 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextAssertTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import org.junit.Rule; import org.junit.Test; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableApplicationContextTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java similarity index 92% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableApplicationContextTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java index 9d1e4a7ca0..975bacd349 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableApplicationContextTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableApplicationContextTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import org.junit.Test; @@ -27,7 +27,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link AssertableApplicationContext}. * * @author Phillip Webb - * @see AssertProviderApplicationContextTests + * @see ApplicationContextAssertProviderTests */ public class AssertableApplicationContextTests { diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContextTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java similarity index 92% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContextTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java index 4f5b49196d..c190b43af5 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableReactiveWebApplicationContextTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContextTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import org.junit.Test; @@ -27,7 +27,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link AssertableReactiveWebApplicationContext}. * * @author Phillip Webb - * @see AssertProviderApplicationContextTests + * @see ApplicationContextAssertProviderTests */ public class AssertableReactiveWebApplicationContextTests { diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableWebApplicationContextTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java similarity index 92% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableWebApplicationContextTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java index b49effc0a9..c88c5f0ba0 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AssertableWebApplicationContextTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContextTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.assertj; import org.junit.Test; @@ -27,7 +27,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link AssertableWebApplicationContext}. * * @author Phillip Webb - * @see AssertProviderApplicationContextTests + * @see ApplicationContextAssertProviderTests */ public class AssertableWebApplicationContextTests { diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java similarity index 94% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java index 73fa1508aa..835ca1576a 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractApplicationContextRunnerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import java.util.UUID; @@ -24,6 +24,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.boot.context.annotation.UserConfigurations; +import org.springframework.boot.test.context.HidePackagesClassLoader; +import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -42,10 +44,10 @@ import static org.assertj.core.api.Assertions.fail; * @author Stephane Nicoll * @author Phillip Webb */ -public abstract class AbstractApplicationContextRunnerTests, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext> { +public abstract class AbstractApplicationContextRunnerTests, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { @Rule - public final ExpectedException thrown = ExpectedException.none(); + public ExpectedException thrown = ExpectedException.none(); @Test public void runWithSystemPropertiesShouldSetAndRemoveProperties() { diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ApplicationContextRunnerTests.java similarity index 88% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ApplicationContextRunnerTests.java index 408abf753f..fd0196f9ba 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ApplicationContextRunnerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ApplicationContextRunnerTests.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.context.ConfigurableApplicationContext; /** diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunnerTests.java similarity index 88% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunnerTests.java index 7ddf7ee134..39024908d4 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ReactiveWebApplicationContextRunnerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunnerTests.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; +import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext; /** diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/WebApplicationContextRunnerTests.java similarity index 91% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/WebApplicationContextRunnerTests.java index 065306fc9b..b461d3070d 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/WebApplicationContextRunnerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/WebApplicationContextRunnerTests.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.boot.test.context; +package org.springframework.boot.test.context.runner; import org.junit.Test; +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.ConfigurableWebApplicationContext; From ad9f28110c7d2c2e73e8a98ae5f68d8f11c64660 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 26 Jul 2017 15:38:58 -0700 Subject: [PATCH 3/4] Make TestPropertyValues immutable Update `TestPropertyValues` so that it is totally immutable. Methods now return a new instance rather than changing existing state. See gh-9875 --- ...o4jRepositoriesAutoConfigurationTests.java | 2 +- .../jdbc/DataSourceJmxConfigurationTests.java | 2 +- .../HikariDataSourceConfigurationTests.java | 4 +- .../AbstractJpaAutoConfigurationTests.java | 2 +- .../AbstractApplicationContextRunner.java | 37 +---- .../boot/test/util/TestPropertyValues.java | 155 ++++++++++-------- ...AbstractApplicationContextRunnerTests.java | 2 +- .../test/util/TestPropertyValuesTests.java | 6 +- 8 files changed, 103 insertions(+), 107 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/MixedNeo4jRepositoriesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/MixedNeo4jRepositoriesAutoConfigurationTests.java index 3c66694189..a6da511cc8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/MixedNeo4jRepositoriesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/neo4j/MixedNeo4jRepositoriesAutoConfigurationTests.java @@ -93,7 +93,7 @@ public class MixedNeo4jRepositoriesAutoConfigurationTests { private void load(Class config, String... environment) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).and("spring.datasource.initialize", "false") + TestPropertyValues.of(environment).and("spring.datasource.initialize=false") .applyTo(context); context.register(config); context.register(DataSourceAutoConfiguration.class, diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java index 1bdac7960f..20d13f518b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java @@ -130,7 +130,7 @@ public class DataSourceJmxConfigurationTests { private void load(Class config, String... environment) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); String jdbcUrl = "jdbc:hsqldb:mem:test-" + UUID.randomUUID().toString(); - TestPropertyValues.of(environment).and("spring.datasource.url", jdbcUrl) + TestPropertyValues.of(environment).and("spring.datasource.url=" + jdbcUrl) .applyTo(context); if (config != null) { context.register(config); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HikariDataSourceConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HikariDataSourceConfigurationTests.java index 6c45a83e5a..087507e7ad 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HikariDataSourceConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/HikariDataSourceConfigurationTests.java @@ -104,8 +104,8 @@ public class HikariDataSourceConfigurationTests { private void load(String... environment) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - TestPropertyValues.of(environment).and("spring.datasource.initialize", "false") - .and("spring.datasource.type", HikariDataSource.class.getName()) + TestPropertyValues.of(environment).and("spring.datasource.initialize=false") + .and("spring.datasource.type=" + HikariDataSource.class.getName()) .applyTo(ctx); ctx.register(DataSourceAutoConfiguration.class); ctx.refresh(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java index 9fa650b48b..f57abf69ad 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java @@ -213,7 +213,7 @@ public abstract class AbstractJpaAutoConfigurationTests { ctx.setClassLoader(classLoader); } TestPropertyValues.of(environment) - .and("spring.datasource.generate-unique-name", "true").applyTo(ctx); + .and("spring.datasource.generate-unique-name=true").applyTo(ctx); ctx.register(TestConfiguration.class); if (!ObjectUtils.isEmpty(configs)) { ctx.register(configs); diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index 270beff416..f22f6f7a81 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -17,7 +17,6 @@ package org.springframework.boot.test.context.runner; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.function.Supplier; @@ -99,9 +98,9 @@ abstract class AbstractApplicationContextRunner contextFactory; - private final TestPropertyValues environmentProperties; + private TestPropertyValues environmentProperties; - private final TestPropertyValues systemProperties; + private TestPropertyValues systemProperties; private ClassLoader classLoader; @@ -131,20 +130,7 @@ abstract class AbstractApplicationContextRunner properties = new LinkedHashMap<>(); + private static final TestPropertyValues EMPTY = new TestPropertyValues( + Collections.emptyMap()); - private TestPropertyValues(String[] pairs) { - addProperties(pairs); - } + private final Map properties; - private void addProperties(String[] pairs) { - for (String pair : pairs) { - and(pair); - } + private TestPropertyValues(Map properties) { + this.properties = Collections.unmodifiableMap(properties); } /** - * Builder method to append another property pair the underlying map of properties. - * @param pair The property pair to add - * @return the existing instance of {@link TestPropertyValues} + * Builder method to add more properties. + * @param pairs The property pairs to add + * @return a new {@link TestPropertyValues} instance */ - public TestPropertyValues and(String pair) { - int index = getSeparatorIndex(pair); - String key = pair.substring(0, index > 0 ? index : pair.length()); - String value = index > 0 ? pair.substring(index + 1) : ""; - and(key.trim(), value.trim()); - return this; + public TestPropertyValues and(String... pairs) { + return and(Arrays.stream(pairs).map(Pair::parse)); } - private int getSeparatorIndex(String pair) { - int colonIndex = pair.indexOf(":"); - int equalIndex = pair.indexOf("="); - if (colonIndex == -1) { - return equalIndex; - } - if (equalIndex == -1) { - return colonIndex; - } - return Math.min(colonIndex, equalIndex); - } - - /** - * Builder method to append another property to the underlying map of properties. - * @param name The property name - * @param value The property value - * @return the existing instance of {@link TestPropertyValues} - */ - public TestPropertyValues and(String name, String value) { - if (StringUtils.isEmpty(name) && StringUtils.isEmpty(value)) { - return this; - } - Assert.hasLength(name, "Name must not be empty"); - this.properties.put(name, value); - return this; + private TestPropertyValues and(Stream pairs) { + Map properties = new LinkedHashMap<>(this.properties); + pairs.filter(Objects::nonNull).forEach((pair) -> pair.addTo(properties)); + return new TestPropertyValues(properties); } /** @@ -170,30 +146,21 @@ public final class TestPropertyValues { return; } } - MapPropertySource source = (type.equals(Type.MAP) - ? new MapPropertySource(name, this.properties) - : new SystemEnvironmentPropertySource(name, this.properties)); - sources.addFirst(source); - } - - /** - * Return a new empty {@link TestPropertyValues} instance. - * @return an empty instance - */ - public static TestPropertyValues empty() { - return of(); + Map source = new LinkedHashMap<>(this.properties); + sources.addFirst((type.equals(Type.MAP) ? new MapPropertySource(name, source) + : new SystemEnvironmentPropertySource(name, source))); } /** * Return a new {@link TestPropertyValues} with the underlying map populated with the - * given property pair. - * @param name the property name - * @param value the property value + * given property pairs. Name-value pairs can be specified with colon (":") or equals + * ("=") separators. + * @param pairs The key value pairs for properties that need to be added to the + * environment * @return the new instance */ - - public static TestPropertyValues ofPair(String name, String value) { - return of().and(name, value); + public static TestPropertyValues of(String... pairs) { + return of(Stream.of(pairs)); } /** @@ -206,9 +173,9 @@ public final class TestPropertyValues { */ public static TestPropertyValues of(Iterable pairs) { if (pairs == null) { - return of(); + return empty(); } - return of(Streams.stream(pairs).toArray(String[]::new)); + return of(Streams.stream(pairs)); } /** @@ -219,8 +186,19 @@ public final class TestPropertyValues { * environment * @return the new instance */ - public static TestPropertyValues of(String... pairs) { - return new TestPropertyValues(pairs); + public static TestPropertyValues of(Stream pairs) { + if (pairs == null) { + return empty(); + } + return empty().and(pairs.map(Pair::parse)); + } + + /** + * Return a new empty {@link TestPropertyValues} instance. + * @return an empty instance + */ + public static TestPropertyValues empty() { + return EMPTY; } /** @@ -250,6 +228,53 @@ public final class TestPropertyValues { } + /** + * A single name value pair. + */ + public static class Pair { + + private String name; + + private String value; + + public Pair(String name, String value) { + Assert.hasLength(name, "Name must not be empty"); + this.name = name; + this.value = value; + } + + public void addTo(Map properties) { + properties.put(this.name, this.value); + } + + public static Pair parse(String pair) { + int index = getSeparatorIndex(pair); + String key = pair.substring(0, index > 0 ? index : pair.length()); + String value = index > 0 ? pair.substring(index + 1) : ""; + return of(key.trim(), value.trim()); + } + + private static int getSeparatorIndex(String pair) { + int colonIndex = pair.indexOf(":"); + int equalIndex = pair.indexOf("="); + if (colonIndex == -1) { + return equalIndex; + } + if (equalIndex == -1) { + return colonIndex; + } + return Math.min(colonIndex, equalIndex); + } + + private static Pair of(String name, String value) { + if (StringUtils.isEmpty(name) && StringUtils.isEmpty(value)) { + return null; + } + return new Pair(name, value); + } + + } + /** * Handler to apply and restore system properties. */ @@ -278,7 +303,7 @@ public final class TestPropertyValues { private String setOrClear(String name, String value) { Assert.notNull(name, "Name must not be null"); - if (value == null) { + if (StringUtils.isEmpty(value)) { return (String) System.getProperties().remove(name); } return (String) System.getProperties().setProperty(name, value); diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java index 835ca1576a..ce0d95e36f 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java @@ -95,7 +95,7 @@ public abstract class AbstractApplicationContextRunnerTests { + get().withSystemProperties(key + "=").run((loaded) -> { assertThat(System.getProperties()).doesNotContainKey(key); }); assertThat(System.getProperties().getProperty(key)).isEqualTo("value"); diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java index e5f7a238fe..3d97c5ec86 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java @@ -89,8 +89,8 @@ public class TestPropertyValuesTests { @Test public void andShouldChainAndAddSingleKeyValue() throws Exception { - TestPropertyValues.of("foo.bar=baz").and("hello.world", "hi") - .and("bling.blah", "bing").applyTo(this.environment, Type.MAP); + TestPropertyValues.of("foo.bar=baz").and("hello.world=hi").and("bling.blah=bing") + .applyTo(this.environment, Type.MAP); assertThat(this.environment.getProperty("foo.bar")).isEqualTo("baz"); assertThat(this.environment.getProperty("hello.world")).isEqualTo("hi"); assertThat(this.environment.getProperty("bling.blah")).isEqualTo("bing"); @@ -127,7 +127,7 @@ public class TestPropertyValuesTests { throws Exception { System.setProperty("foo", "bar1"); try { - TestPropertyValues.ofPair("foo", null).applyToSystemProperties(() -> { + TestPropertyValues.of("foo").applyToSystemProperties(() -> { assertThat(System.getProperties()).doesNotContainKey("foo"); return null; }); From 89ad0660d13fa880e41ed3605d7d9847d6b19ec6 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 26 Jul 2017 16:37:42 -0700 Subject: [PATCH 4/4] Make ApplicationContextRunner immutable Update `ApplicationContextRunner` so that it is totally immutable. Methods now return new instances rather than changing existing state. See gh-9875 --- .../ArtemisAutoConfigurationTests.java | 18 ++-- .../TestDatabaseAutoConfigurationTests.java | 4 +- .../AbstractApplicationContextRunner.java | 90 +++++++++++++------ .../runner/ApplicationContextRunner.java | 23 +++++ .../ReactiveWebApplicationContextRunner.java | 24 +++++ .../runner/WebApplicationContextRunner.java | 23 +++++ 6 files changed, 145 insertions(+), 37 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index bcd71ba404..2d1a767f24 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -231,15 +231,15 @@ public class ArtemisAutoConfigurationTests { "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath()) .run((context) -> context.getBean(JmsTemplate.class).send("TestQueue", - (session) -> session.createTextMessage(messageId))); - // Start the server again and check if our message is still here - this.contextRunner.run((context) -> { - JmsTemplate jmsTemplate2 = context.getBean(JmsTemplate.class); - jmsTemplate2.setReceiveTimeout(1000L); - Message message = jmsTemplate2.receive("TestQueue"); - assertThat(message).isNotNull(); - assertThat(((TextMessage) message).getText()).isEqualTo(messageId); - }); + (session) -> session.createTextMessage(messageId))) + .run((context) -> { + // Start the server again and check if our message is still here + JmsTemplate jmsTemplate2 = context.getBean(JmsTemplate.class); + jmsTemplate2.setReceiveTimeout(1000L); + Message message = jmsTemplate2.receive("TestQueue"); + assertThat(message).isNotNull(); + assertThat(((TextMessage) message).getText()).isEqualTo(messageId); + }); } @Test diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java index b688c00c64..1ab50dcb73 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java @@ -55,7 +55,9 @@ public class TestDatabaseAutoConfigurationTests { DataSource datasource = context.getBean(DataSource.class); JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource); jdbcTemplate.execute("create table example (id int, name varchar);"); - this.contextRunner.run((secondContext) -> { + this.contextRunner + .withUserConfiguration(ExistingDataSourceConfiguration.class) + .run((secondContext) -> { DataSource anotherDatasource = secondContext .getBean(DataSource.class); JdbcTemplate anotherJdbcTemplate = new JdbcTemplate( diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index f22f6f7a81..37d54d125c 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.context.runner; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Supplier; @@ -98,25 +99,48 @@ abstract class AbstractApplicationContextRunner contextFactory; - private TestPropertyValues environmentProperties; + private final TestPropertyValues environmentProperties; - private TestPropertyValues systemProperties; + private final TestPropertyValues systemProperties; - private ClassLoader classLoader; + private final ClassLoader classLoader; - private ApplicationContext parent; + private final ApplicationContext parent; - private final List configurations = new ArrayList<>(); + private final List configurations; /** * Create a new {@link AbstractApplicationContextRunner} instance. * @param contextFactory the factory used to create the actual context */ protected AbstractApplicationContextRunner(Supplier contextFactory) { + this(contextFactory, TestPropertyValues.empty(), TestPropertyValues.empty(), null, + null, Collections.emptyList()); + } + + /** + * Create a new {@link AbstractApplicationContextRunner} instance. + * @param contextFactory the factory used to create the actual context + * @param environmentProperties the environment properties + * @param systemProperties the system properties + * @param classLoader the class loader + * @param parent the parent + * @param configurations the configuration + */ + protected AbstractApplicationContextRunner(Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { Assert.notNull(contextFactory, "ContextFactory must not be null"); + Assert.notNull(environmentProperties, "EnvironmentProperties must not be null"); + Assert.notNull(systemProperties, "SystemProperties must not be null"); + Assert.notNull(configurations, "Configurations must not be null"); this.contextFactory = contextFactory; - this.environmentProperties = TestPropertyValues.empty(); - this.systemProperties = TestPropertyValues.empty(); + this.environmentProperties = environmentProperties; + this.systemProperties = systemProperties; + this.classLoader = classLoader; + this.parent = parent; + this.configurations = Collections.unmodifiableList(configurations); } /** @@ -125,13 +149,14 @@ abstract class AbstractApplicationContextRunner... configurationClasses) { return withConfiguration(UserConfigurations.of(configurationClasses)); @@ -186,32 +212,42 @@ abstract class AbstractApplicationContextRunner List add(List list, T element) { + List result = new ArrayList<>(list); + result.add(element); + return result; } + protected abstract SELF newInstance(Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations); + /** * Create and refresh a new {@link ApplicationContext} based on the current state of * this loader. The context is consumed by the specified {@code consumer} and closed * upon completion. * @param consumer the consumer of the created {@link ApplicationContext} + * @return this instance */ - public void run(ContextConsumer consumer) { + @SuppressWarnings("unchecked") + public SELF run(ContextConsumer consumer) { this.systemProperties.applyToSystemProperties(() -> { try (A context = createAssertableContext()) { accept(consumer, context); } return null; }); + return (SELF) this; } @SuppressWarnings("unchecked") diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java index d3799554bd..52278569b9 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java @@ -16,9 +16,13 @@ package org.springframework.boot.test.context.runner; +import java.util.List; import java.util.function.Supplier; +import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -54,4 +58,23 @@ public class ApplicationContextRunner extends super(contextFactory); } + private ApplicationContextRunner( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + super(contextFactory, environmentProperties, systemProperties, classLoader, + parent, configurations); + } + + @Override + protected ApplicationContextRunner newInstance( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + return new ApplicationContextRunner(contextFactory, environmentProperties, + systemProperties, classLoader, parent, configurations); + } + } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java index 07ec6fc2fd..431682e2f5 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java @@ -16,11 +16,15 @@ package org.springframework.boot.test.context.runner; +import java.util.List; import java.util.function.Supplier; +import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; +import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; +import org.springframework.context.ApplicationContext; /** * A {@link AbstractApplicationContextRunner ApplicationContext runner} for a @@ -54,4 +58,24 @@ public final class ReactiveWebApplicationContextRunner extends super(contextFactory); } + private ReactiveWebApplicationContextRunner( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + super(contextFactory, environmentProperties, systemProperties, classLoader, + parent, configurations); + } + + @Override + protected ReactiveWebApplicationContextRunner newInstance( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + return new ReactiveWebApplicationContextRunner(contextFactory, + environmentProperties, systemProperties, classLoader, parent, + configurations); + } + } diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java index ea3e9b1c54..beb245bb2d 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java @@ -16,9 +16,13 @@ package org.springframework.boot.test.context.runner; +import java.util.List; import java.util.function.Supplier; +import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContext; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; @@ -58,6 +62,25 @@ public final class WebApplicationContextRunner extends super(contextFactory); } + private WebApplicationContextRunner( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + super(contextFactory, environmentProperties, systemProperties, classLoader, + parent, configurations); + } + + @Override + protected WebApplicationContextRunner newInstance( + Supplier contextFactory, + TestPropertyValues environmentProperties, TestPropertyValues systemProperties, + ClassLoader classLoader, ApplicationContext parent, + List configurations) { + return new WebApplicationContextRunner(contextFactory, environmentProperties, + systemProperties, classLoader, parent, configurations); + } + /** * Decorate the specified {@code contextFactory} to set a {@link MockServletContext} * on each newly created {@link WebApplicationContext}.