Rename ApplicationContextTester -> Runner

Rename `ApplicationContextTester` and related classes to
`ApplicationContextRunner` and refactor existing tests to use correctly
named variables.

See gh-9875
pull/7499/merge
Phillip Webb 7 years ago
parent 5616915621
commit 497457c397

@ -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<String, HealthIndicator> beans = loaded
.run((context) -> {
Map<String, HealthIndicator> 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<String, HealthIndicator> beans = loaded
this.contextRunner
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class,
RedisAutoConfiguration.class, MongoDataAutoConfiguration.class,
SolrAutoConfiguration.class))
.run((context) -> {
Map<String, HealthIndicator> 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<String, HealthIndicator> beans = loaded
.run((context) -> {
Map<String, HealthIndicator> 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<String, HealthIndicator> beans = loaded
.run((context) -> {
Map<String, HealthIndicator> 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<AssertableApplicationContext> hasSingleHealthIndicator(
Class<? extends HealthIndicator> 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"));

@ -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<AssertableApplicationContext> dunno(
String... expectedCustomizerNames) {
return (loaded) -> {
CacheManager cacheManager = getCacheManager(loaded, CacheManager.class);
return (context) -> {
CacheManager cacheManager = getCacheManager(context, CacheManager.class);
List<String> expected = new ArrayList<>(
Arrays.asList(expectedCustomizerNames));
Map<String, CacheManagerTestCustomizer> customizer = loaded
Map<String, CacheManagerTestCustomizer> customizer = context
.getBeansOfType(CacheManagerTestCustomizer.class);
customizer.forEach((key, value) -> {
if (expected.contains(key)) {

@ -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));
});
}

@ -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);
});
}

@ -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<String, QueueConfig> queueConfigs = config.getQueueConfigs();
assertThat(queueConfigs.keySet()).containsOnly("another-queue");
});

@ -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());
});

@ -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)

@ -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<String> hiddenPackages, Consumer<T> 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));
});

@ -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(

@ -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();
});
}

@ -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",

@ -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);
});
}

@ -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<String, List<Resource>> locations = getResourceMappingLocations(loaded);
this.contextRunner.run((context) -> {
Map<String, List<Resource>> 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<String, List<Resource>> 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<String, List<Resource>> locations = getResourceMappingLocations(loaded);
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {
Map<String, List<Resource>> 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<String, List<Resource>> locations = getResourceMappingLocations(loaded);
this.contextRunner.withUserConfiguration(AllResources.class).run((context) -> {
Map<String, List<Resource>> 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<String, List<Resource>> 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<String, VersionStrategy> 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<AssertableWebApplicationContext> assertExceptionResolverWarnLoggers(
Consumer<Object> consumer) {
return (loaded) -> {
HandlerExceptionResolver resolver = loaded
return (context) -> {
HandlerExceptionResolver resolver = context
.getBean(HandlerExceptionResolver.class);
assertThat(resolver).isInstanceOf(HandlerExceptionResolverComposite.class);
List<HandlerExceptionResolver> 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<String, List<Resource>> getFaviconMappingLocations(

@ -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<String> getUrlMappings(ApplicationContext loaded) {
return getServletRegistrationBean(loaded).getUrlMappings();
private Collection<String> getUrlMappings(ApplicationContext context) {
return getServletRegistrationBean(context).getUrlMappings();
}
private ServletRegistrationBean<?> getServletRegistrationBean(

@ -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(

@ -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"));
});
}

@ -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:
*
* <pre class="code">
* public class MyContextTests {
* private final ApplicationContextTester context = new ApplicationContextTester()
* private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
* .withPropertyValues("spring.foo=bar")
* .withUserConfiguration(MyConfiguration.class);
* }</pre>
@ -55,8 +55,8 @@ import org.springframework.util.ReflectionUtils;
* <pre class="code">
* &#064;Test
* public someTest() {
* this.context.withPropertyValues("spring.foo=biz").run((loaded) -&gt; {
* assertThat(loaded).containsSingleBean(MyBean.class);
* this.contextRunner.withPropertyValues("spring.foo=biz").run((context) -&gt; {
* assertThat(context).containsSingleBean(MyBean.class);
* // other assertions
* });
* }</pre>
@ -80,19 +80,19 @@ import org.springframework.util.ReflectionUtils;
* }</pre>
* <p>
*
* @param <SELF> The "self" type for this tester
* @param <SELF> The "self" type for this runner
* @param <C> The context type
* @param <A> 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<SELF extends AbstractApplicationContextTester<SELF, C, A>, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext<C>> {
abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF, C, A>, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext<C>> {
private final Supplier<C> contextFactory;
@ -107,10 +107,10 @@ abstract class AbstractApplicationContextTester<SELF extends AbstractApplication
private final List<Configurations> 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<C> contextFactory) {
protected AbstractApplicationContextRunner(Supplier<C> contextFactory) {
Assert.notNull(contextFactory, "ContextFactory must not be null");
this.contextFactory = contextFactory;
this.environmentProperties = TestPropertyValues.empty();
@ -243,7 +243,7 @@ abstract class AbstractApplicationContextTester<SELF extends AbstractApplication
@SuppressWarnings("unchecked")
private A createAssertableContext() {
ResolvableType resolvableType = ResolvableType
.forClass(AbstractApplicationContextTester.class, getClass());
.forClass(AbstractApplicationContextRunner.class, getClass());
Class<A> assertType = (Class<A>) resolvableType.resolveGeneric(1);
Class<C> contextType = (Class<C>) resolvableType.resolveGeneric(2);
return AssertProviderApplicationContext.get(assertType, contextType,

@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @param <C> The application context type
* @author Phillip Webb
* @since 2.0.0
* @see ApplicationContextTester
* @see ApplicationContextRunner
* @see AssertableApplicationContext
*/
public class ApplicationContextAssert<C extends ApplicationContext>

@ -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}.
* <p>
* 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<ApplicationContextTester, ConfigurableApplicationContext, AssertableApplicationContext> {
public class ApplicationContextRunner extends
AbstractApplicationContextRunner<ApplicationContextRunner, ConfigurableApplicationContext, AssertableApplicationContext> {
/**
* 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<ConfigurableApplicationContext> contextFactory) {
super(contextFactory);
}

@ -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

@ -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

@ -26,7 +26,7 @@ import org.springframework.context.ApplicationContext;
* @author Andy Wilkinson
* @param <C> The application context type
* @since 2.0.0
* @see AbstractApplicationContextTester
* @see AbstractApplicationContextRunner
*/
@FunctionalInterface
public interface ContextConsumer<C extends ApplicationContext> {

@ -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}.
* <p>
* 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<ReactiveWebApplicationContextTester, ConfigurableReactiveWebApplicationContext, AssertableReactiveWebApplicationContext> {
public final class ReactiveWebApplicationContextRunner extends
AbstractApplicationContextRunner<ReactiveWebApplicationContextRunner, ConfigurableReactiveWebApplicationContext, AssertableReactiveWebApplicationContext> {
/**
* 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<ConfigurableReactiveWebApplicationContext> contextFactory) {
super(contextFactory);
}

@ -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}.
* <p>
* 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<WebApplicationContextTester, ConfigurableWebApplicationContext, AssertableWebApplicationContext> {
public final class WebApplicationContextRunner extends
AbstractApplicationContextRunner<WebApplicationContextRunner, ConfigurableWebApplicationContext, AssertableWebApplicationContext> {
/**
* 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<ConfigurableWebApplicationContext> contextFactory) {
super(contextFactory);
}

@ -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 <T> The tester type
* @param <T> The runner type
* @param <C> the context type
* @param <A> the assertable context type
* @author Stephane Nicoll
* @author Phillip Webb
*/
public abstract class AbstractApplicationContextTesterTests<T extends AbstractApplicationContextTester<T, C, A>, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext<C>> {
public abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicationContextRunner<T, C, A>, C extends ConfigurableApplicationContext, A extends AssertProviderApplicationContext<C>> {
@Rule
public final ExpectedException thrown = ExpectedException.none();

@ -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<ApplicationContextTester, ConfigurableApplicationContext, AssertableApplicationContext> {
public class ApplicationContextRunnerTests extends
AbstractApplicationContextRunnerTests<ApplicationContextRunner, ConfigurableApplicationContext, AssertableApplicationContext> {
@Override
protected ApplicationContextTester get() {
return new ApplicationContextTester();
protected ApplicationContextRunner get() {
return new ApplicationContextRunner();
}
}

@ -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<ReactiveWebApplicationContextTester, ConfigurableReactiveWebApplicationContext, AssertableReactiveWebApplicationContext> {
public class ReactiveWebApplicationContextRunnerTests extends
AbstractApplicationContextRunnerTests<ReactiveWebApplicationContextRunner, ConfigurableReactiveWebApplicationContext, AssertableReactiveWebApplicationContext> {
@Override
protected ReactiveWebApplicationContextTester get() {
return new ReactiveWebApplicationContextTester();
protected ReactiveWebApplicationContextRunner get() {
return new ReactiveWebApplicationContextRunner();
}
}

@ -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<WebApplicationContextTester, ConfigurableWebApplicationContext, AssertableWebApplicationContext> {
public class WebApplicationContextRunnerTests extends
AbstractApplicationContextRunnerTests<WebApplicationContextRunner, ConfigurableWebApplicationContext, AssertableWebApplicationContext> {
@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();
}
}
Loading…
Cancel
Save