SELF
- the "self" type for this runnerC
- the context typeA
- the application context assertion providerpublic abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>> extends Object
ApplicationContext
and provide AssertJ style
assertions. The test is best used as a field of a test class, describing the shared
configuration required for the test:
public class MyContextTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withPropertyValues("spring.foo=bar") .withUserConfiguration(MyConfiguration.class); }
The initialization above makes sure to register MyConfiguration
for all tests
and set the spring.foo
property to bar
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 to biz
and is
asserting that the context contains a single MyBean
bean. The
run
method takes a ContextConsumer
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 an
IllegalStateException
and assertions that expect a running context will fail.
The getFailure()
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 }); }
ApplicationContextRunner
,
WebApplicationContextRunner
,
ReactiveWebApplicationContextRunner
,
ApplicationContextAssert
Modifier and Type | Class and Description |
---|---|
protected class |
AbstractApplicationContextRunner.BeanRegistration<T>
A Bean registration to be applied when the context loaded.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractApplicationContextRunner(Supplier<C> contextFactory)
Create a new
AbstractApplicationContextRunner instance. |
protected |
AbstractApplicationContextRunner(Supplier<C> contextFactory,
List<ApplicationContextInitializer<? super C>> initializers,
TestPropertyValues environmentProperties,
TestPropertyValues systemProperties,
ClassLoader classLoader,
ApplicationContext parent,
List<AbstractApplicationContextRunner.BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations)
Create a new
AbstractApplicationContextRunner instance. |
Modifier and Type | Method and Description |
---|---|
protected abstract SELF |
newInstance(Supplier<C> contextFactory,
List<ApplicationContextInitializer<? super C>> initializers,
TestPropertyValues environmentProperties,
TestPropertyValues systemProperties,
ClassLoader classLoader,
ApplicationContext parent,
List<AbstractApplicationContextRunner.BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) |
SELF |
run(ContextConsumer<? super A> consumer)
Create and refresh a new
ApplicationContext based on the current state of
this loader. |
SELF |
with(Function<SELF,SELF> customizer)
Apply customization to this runner.
|
<T> SELF |
withBean(Class<T> type,
Object... constructorArgs)
Register the specified user bean with the
ApplicationContext . |
<T> SELF |
withBean(Class<T> type,
Supplier<T> supplier,
BeanDefinitionCustomizer... customizers)
Register the specified user bean with the
ApplicationContext . |
<T> SELF |
withBean(String name,
Class<T> type,
Object... constructorArgs)
Register the specified user bean with the
ApplicationContext . |
<T> SELF |
withBean(String name,
Class<T> type,
Supplier<T> supplier,
BeanDefinitionCustomizer... customizers)
Register the specified user bean with the
ApplicationContext . |
SELF |
withClassLoader(ClassLoader classLoader)
Customize the
ClassLoader that the ApplicationContext should use
for resource loading and bean class loading. |
SELF |
withConfiguration(Configurations configurations)
Register the specified configuration classes with the
ApplicationContext . |
SELF |
withInitializer(ApplicationContextInitializer<? super C> initializer)
Add a
ApplicationContextInitializer to be called when the context is
created. |
SELF |
withParent(ApplicationContext parent)
Configure the
parent of the ApplicationContext . |
SELF |
withPropertyValues(String... pairs)
Add the specified
Environment property pairs. |
SELF |
withSystemProperties(String... pairs)
Add the specified
System property pairs. |
SELF |
withUserConfiguration(Class<?>... configurationClasses)
Register the specified user configuration classes with the
ApplicationContext . |
protected AbstractApplicationContextRunner(Supplier<C> contextFactory)
AbstractApplicationContextRunner
instance.contextFactory
- the factory used to create the actual contextprotected AbstractApplicationContextRunner(Supplier<C> contextFactory, List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List<AbstractApplicationContextRunner.BeanRegistration<?>> beanRegistrations, List<Configurations> configurations)
AbstractApplicationContextRunner
instance.contextFactory
- the factory used to create the actual contextinitializers
- the initializersenvironmentProperties
- the environment propertiessystemProperties
- the system propertiesclassLoader
- the class loaderparent
- the parentbeanRegistrations
- the bean registrationsconfigurations
- the configurationpublic SELF withInitializer(ApplicationContextInitializer<? super C> initializer)
ApplicationContextInitializer
to be called when the context is
created.initializer
- the initializer to addpublic SELF withPropertyValues(String... pairs)
Environment
property pairs. Key-value pairs can be
specified with colon (":") or equals ("=") separators. Override matching keys that
might have been specified previously.pairs
- the key-value pairs for properties that need to be added to the
environmentTestPropertyValues
,
withSystemProperties(String...)
public SELF withSystemProperties(String... pairs)
System
property pairs. Key-value pairs can be specified
with colon (":") or equals ("=") separators. System properties are added before the
context is run
and restored when the context is
closed.pairs
- the key-value pairs for properties that need to be added to the systemTestPropertyValues
,
withSystemProperties(String...)
public SELF withClassLoader(ClassLoader classLoader)
ClassLoader
that the ApplicationContext
should use
for resource loading and bean class loading.classLoader
- the classloader to use (can be null to use the default)FilteredClassLoader
public SELF withParent(ApplicationContext parent)
parent
of the ApplicationContext
.parent
- the parentpublic <T> SELF withBean(Class<T> type, Object... constructorArgs)
ApplicationContext
. The bean name
is generated from the configured BeanNameGenerator
on the underlying
context.
Such beans are registered after regular user configurations in the order of registration.
T
- the type of the beantype
- the type of the beanconstructorArgs
- custom argument values to be fed into Spring's constructor
resolution algorithm, resolving either all arguments or just specific ones, with
the rest to be resolved through regular autowiring (may be null
or empty)public <T> SELF withBean(String name, Class<T> type, Object... constructorArgs)
ApplicationContext
.
Such beans are registered after regular user configurations in the order of registration.
T
- the type of the beanname
- the bean name or null
to use a generated nametype
- the type of the beanconstructorArgs
- custom argument values to be fed into Spring's constructor
resolution algorithm, resolving either all arguments or just specific ones, with
the rest to be resolved through regular autowiring (may be null
or empty)public <T> SELF withBean(Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)
ApplicationContext
. The bean name
is generated from the configured BeanNameGenerator
on the underlying
context.
Such beans are registered after regular user configurations in the order of registration.
T
- the type of the beantype
- the type of the beansupplier
- a supplier for the beancustomizers
- one or more callbacks for customizing the factory's
BeanDefinition
, e.g. setting a lazy-init or primary flagpublic <T> SELF withBean(String name, Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)
ApplicationContext
. The bean name
is generated from the configured BeanNameGenerator
on the underlying
context.
Such beans are registered after regular user configurations in the order of registration.
T
- the type of the beanname
- the bean name or null
to use a generated nametype
- the type of the beansupplier
- a supplier for the beancustomizers
- one or more callbacks for customizing the factory's
BeanDefinition
, e.g. setting a lazy-init or primary flagpublic SELF withUserConfiguration(Class<?>... configurationClasses)
ApplicationContext
.configurationClasses
- the user configuration classes to addpublic SELF withConfiguration(Configurations configurations)
ApplicationContext
.configurations
- the configurations to addpublic SELF with(Function<SELF,SELF> customizer)
customizer
- the customizer to callprotected abstract SELF newInstance(Supplier<C> contextFactory, List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List<AbstractApplicationContextRunner.BeanRegistration<?>> beanRegistrations, List<Configurations> configurations)
public SELF run(ContextConsumer<? super A> consumer)
ApplicationContext
based on the current state of
this loader. The context is consumed by the specified consumer
and closed
upon completion.consumer
- the consumer of the created ApplicationContext
Copyright © 2020 Pivotal Software, Inc.. All rights reserved.