Class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends org.springframework.context.ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>>
- java.lang.Object
- org.springframework.boot.test.context.runner.AbstractApplicationContextRunner<SELF,C,A>
- Type Parameters:
SELF
- the "self" type for this runnerC
- the context typeA
- the application context assertion provider
- Direct Known Subclasses:
ApplicationContextRunner
,ReactiveWebApplicationContextRunner
,WebApplicationContextRunner
public abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends org.springframework.context.ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>> extends Object
Utility design to run and anApplicationContext
and provide AssertJ style assertions. The test is best used as a field of a test class, describing the shared configuration required for the test:public class MyContextTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withPropertyValues("spring.foo=bar") .withUserConfiguration(MyConfiguration.class); }
The initialization above makes sure to register
MyConfiguration
for all tests and set thespring.foo
property tobar
unless specified otherwise.Based on the configuration above, a specific test can simulate what will happen when the context runs, perhaps with overridden property values:
@Test public someTest() { this.contextRunner.withPropertyValues("spring.foo=biz").run((context) -> { assertThat(context).containsSingleBean(MyBean.class); // other assertions }); }
The test above has changed the
spring.foo
property tobiz
and is asserting that the context contains a singleMyBean
bean. Therun
method takes aContextConsumer
that can apply assertions to the context. Upon completion, the context is automatically closed.If the application context fails to start the
#run(ContextConsumer)
method is called with a "failed" application context. Calls to the context will throw anIllegalStateException
and assertions that expect a running context will fail. ThegetFailure()
assertion can be used if further checks are required on the cause of the failure:@Test public someTest() { this.context.withPropertyValues("spring.foo=fails").run((loaded) -> { assertThat(loaded).getFailure().hasCauseInstanceOf(BadPropertyException.class); // other assertions }); }
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractApplicationContextRunner(Supplier<C> contextFactory)
Create a newAbstractApplicationContextRunner
instance.protected
AbstractApplicationContextRunner(Supplier<C> contextFactory, List<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, org.springframework.context.ApplicationContext parent, List<org.springframework.boot.context.annotation.Configurations> configurations)
Create a newAbstractApplicationContextRunner
instance.
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract SELF
newInstance(Supplier<C> contextFactory, List<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, org.springframework.context.ApplicationContext parent, List<org.springframework.boot.context.annotation.Configurations> configurations)
SELF
run(ContextConsumer<? super A> consumer)
Create and refresh a newApplicationContext
based on the current state of this loader.SELF
with(Function<SELF,SELF> customizer)
Apply customization to this runner.SELF
withClassLoader(ClassLoader classLoader)
Customize theClassLoader
that theApplicationContext
should use for resource loading and bean class loading.SELF
withConfiguration(org.springframework.boot.context.annotation.Configurations configurations)
Register the specified configuration classes with theApplicationContext
.SELF
withInitializer(org.springframework.context.ApplicationContextInitializer<? super org.springframework.context.ConfigurableApplicationContext> initializer)
Add aApplicationContextInitializer
to be called when the context is created.SELF
withParent(org.springframework.context.ApplicationContext parent)
Configure theparent
of theApplicationContext
.SELF
withPropertyValues(String... pairs)
Add the specifiedEnvironment
property pairs.SELF
withSystemProperties(String... pairs)
Add the specifiedSystem
property pairs.SELF
withUserConfiguration(Class<?>... configurationClasses)
Register the specified user configuration classes with theApplicationContext
.
Constructor Detail
AbstractApplicationContextRunner
protected AbstractApplicationContextRunner(Supplier<C> contextFactory)
Create a newAbstractApplicationContextRunner
instance.- Parameters:
contextFactory
- the factory used to create the actual context
AbstractApplicationContextRunner
protected AbstractApplicationContextRunner(Supplier<C> contextFactory, List<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, org.springframework.context.ApplicationContext parent, List<org.springframework.boot.context.annotation.Configurations> configurations)
Create a newAbstractApplicationContextRunner
instance.- Parameters:
contextFactory
- the factory used to create the actual contextinitializers
- the initializersenvironmentProperties
- the environment propertiessystemProperties
- the system propertiesclassLoader
- the class loaderparent
- the parentconfigurations
- the configuration
Method Detail
withInitializer
public SELF withInitializer(org.springframework.context.ApplicationContextInitializer<? super org.springframework.context.ConfigurableApplicationContext> initializer)
Add aApplicationContextInitializer
to be called when the context is created.- Parameters:
initializer
- the initializer to add- Returns:
- a new instance with the updated initializers
withPropertyValues
public SELF withPropertyValues(String... pairs)
Add the specifiedEnvironment
property pairs. Key-value pairs can be specified with colon (":") or equals ("=") separators. Override matching keys that might have been specified previously.- Parameters:
pairs
- the key-value pairs for properties that need to be added to the environment- Returns:
- a new instance with the updated property values
- See Also:
TestPropertyValues
,withSystemProperties(String...)
withSystemProperties
public SELF withSystemProperties(String... pairs)
Add the specifiedSystem
property pairs. Key-value pairs can be specified with colon (":") or equals ("=") separators. System properties are added before the context isrun
and restored when the context is closed.- Parameters:
pairs
- the key-value pairs for properties that need to be added to the system- Returns:
- a new instance with the updated system properties
- See Also:
TestPropertyValues
,withSystemProperties(String...)
withClassLoader
public SELF withClassLoader(ClassLoader classLoader)
Customize theClassLoader
that theApplicationContext
should use for resource loading and bean class loading.- Parameters:
classLoader
- the classloader to use (can be null to use the default)- Returns:
- a new instance with the updated class loader
- See Also:
FilteredClassLoader
withParent
public SELF withParent(org.springframework.context.ApplicationContext parent)
Configure theparent
of theApplicationContext
.- Parameters:
parent
- the parent- Returns:
- a new instance with the updated parent
withUserConfiguration
public SELF withUserConfiguration(Class<?>... configurationClasses)
Register the specified user configuration classes with theApplicationContext
.- Parameters:
configurationClasses
- the user configuration classes to add- Returns:
- a new instance with the updated configuration
withConfiguration
public SELF withConfiguration(org.springframework.boot.context.annotation.Configurations configurations)
Register the specified configuration classes with theApplicationContext
.- Parameters:
configurations
- the configurations to add- Returns:
- a new instance with the updated configuration
with
public SELF with(Function<SELF,SELF> customizer)
Apply customization to this runner.- Parameters:
customizer
- the customizer to call- Returns:
- a new instance with the customizations applied
newInstance
protected abstract SELF newInstance(Supplier<C> contextFactory, List<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, org.springframework.context.ApplicationContext parent, List<org.springframework.boot.context.annotation.Configurations> configurations)
run
public SELF run(ContextConsumer<? super A> consumer)
Create and refresh a newApplicationContext
based on the current state of this loader. The context is consumed by the specifiedconsumer
and closed upon completion.- Parameters:
consumer
- the consumer of the createdApplicationContext
- Returns:
- this instance