类 AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>>
- 类型参数:
SELF
- the "self" type for this runnerC
- the context typeA
- the application context assertion provider
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 }); }
- 从以下版本开始:
- 2.0.0
- 作者:
- Stephane Nicoll, Andy Wilkinson, Phillip Webb
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明protected static final class
A Bean registration to be applied when the context loaded.protected static final class
-
构造器概要
构造器限定符构造器说明protected
AbstractApplicationContextRunner
(Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory, Supplier<C> contextFactory, Class<?>... additionalContextInterfaces) Create a newAbstractApplicationContextRunner
instance.protected
AbstractApplicationContextRunner
(Supplier<C> contextFactory, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) 已过时, 待删除: 此 API 元素将从以后的版本中删除。protected
AbstractApplicationContextRunner
(AbstractApplicationContextRunner.RunnerConfiguration<C> configuration, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Create a newAbstractApplicationContextRunner
instance. -
方法概要
修饰符和类型方法说明prepare
(ContextConsumer<? super A> consumer) Prepare a newApplicationContext
based on the current state of this loader.run
(ContextConsumer<? super A> consumer) Create and refresh a newApplicationContext
based on the current state of this loader.Apply customization to this runner.withAllowBeanDefinitionOverriding
(boolean allowBeanDefinitionOverriding) Specify if bean definition overriding, by registering a definition with the same name as an existing definition, should be allowed.withAllowCircularReferences
(boolean allowCircularReferences) Specify if circular references between beans should be allowed.<T> SELF
Register the specified user bean with theApplicationContext
.<T> SELF
withBean
(Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext
.<T> SELF
Register the specified user bean with theApplicationContext
.<T> SELF
withBean
(String name, Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext
.withClassLoader
(ClassLoader classLoader) Customize theClassLoader
that theApplicationContext
should use for resource loading and bean class loading.withConfiguration
(Configurations configurations) Register the specified configuration classes with theApplicationContext
.withInitializer
(ApplicationContextInitializer<? super C> initializer) Add anApplicationContextInitializer
to be called when the context is created.withParent
(ApplicationContext parent) Configure theparent
of theApplicationContext
.withPropertyValues
(String... pairs) Add the specifiedEnvironment
property pairs.withSystemProperties
(String... pairs) Add the specifiedSystem
property pairs.withUserConfiguration
(Class<?>... configurationClasses) Register the specified user configuration classes with theApplicationContext
.
-
构造器详细资料
-
AbstractApplicationContextRunner
@Deprecated(since="3.4.0", forRemoval=true) protected AbstractApplicationContextRunner(Supplier<C> contextFactory, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) 已过时, 待删除: 此 API 元素将从以后的版本中删除。since 3.4.0 for removal in 4.0.0 in favor ofAbstractApplicationContextRunner(Function, Supplier, Class...)
Create a newAbstractApplicationContextRunner
instance.- 参数:
contextFactory
- the factory used to create the actual contextinstanceFactory
- the factory used to create new instance of the runner- 从以下版本开始:
- 2.6.0
-
AbstractApplicationContextRunner
protected AbstractApplicationContextRunner(Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory, Supplier<C> contextFactory, Class<?>... additionalContextInterfaces) Create a newAbstractApplicationContextRunner
instance.- 参数:
instanceFactory
- the factory used to create new instance of the runnercontextFactory
- the factory used to create the actual contextadditionalContextInterfaces
- any additional application context interfaces to be added to the application context proxy- 从以下版本开始:
- 3.4.0
-
AbstractApplicationContextRunner
protected AbstractApplicationContextRunner(AbstractApplicationContextRunner.RunnerConfiguration<C> configuration, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Create a newAbstractApplicationContextRunner
instance.- 参数:
configuration
- the configuration for the runner to useinstanceFactory
- the factory used to create new instance of the runner- 从以下版本开始:
- 2.6.0
-
-
方法详细资料
-
withAllowBeanDefinitionOverriding
Specify if bean definition overriding, by registering a definition with the same name as an existing definition, should be allowed.- 参数:
allowBeanDefinitionOverriding
- if bean overriding is allowed- 返回:
- a new instance with the updated bean definition overriding policy
- 从以下版本开始:
- 2.3.0
- 另请参阅:
-
withAllowCircularReferences
Specify if circular references between beans should be allowed.- 参数:
allowCircularReferences
- if circular references between beans are allowed- 返回:
- a new instance with the updated circular references policy
- 从以下版本开始:
- 2.6.0
- 另请参阅:
-
withInitializer
Add anApplicationContextInitializer
to be called when the context is created.- 参数:
initializer
- the initializer to add- 返回:
- a new instance with the updated initializers
-
withPropertyValues
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.- 参数:
pairs
- the key-value pairs for properties that need to be added to the environment- 返回:
- a new instance with the updated property values
- 另请参阅:
-
withSystemProperties
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.- 参数:
pairs
- the key-value pairs for properties that need to be added to the system- 返回:
- a new instance with the updated system properties
- 另请参阅:
-
withClassLoader
Customize theClassLoader
that theApplicationContext
should use for resource loading and bean class loading.- 参数:
classLoader
- the classloader to use (ornull
to use the default)- 返回:
- a new instance with the updated class loader
- 另请参阅:
-
withParent
Configure theparent
of theApplicationContext
.- 参数:
parent
- the parent- 返回:
- a new instance with the updated parent
-
withBean
Register the specified user bean with theApplicationContext
. The bean name is generated from the configuredBeanNameGenerator
on the underlying context.Such beans are registered after regular user configurations in the order of registration.
- 类型参数:
T
- the type of the bean- 参数:
type
- 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 benull
or empty)- 返回:
- a new instance with the updated bean
-
withBean
Register the specified user bean with theApplicationContext
.Such beans are registered after regular user configurations in the order of registration.
- 类型参数:
T
- the type of the bean- 参数:
name
- the bean name ornull
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 benull
or empty)- 返回:
- a new instance with the updated bean
-
withBean
public <T> SELF withBean(Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext
. The bean name is generated from the configuredBeanNameGenerator
on the underlying context.Such beans are registered after regular user configurations in the order of registration.
- 类型参数:
T
- the type of the bean- 参数:
type
- the type of the beansupplier
- a supplier for the beancustomizers
- one or more callbacks for customizing the factory'sBeanDefinition
, e.g. setting a lazy-init or primary flag- 返回:
- a new instance with the updated bean
-
withBean
public <T> SELF withBean(String name, Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext
. The bean name is generated from the configuredBeanNameGenerator
on the underlying context.Such beans are registered after regular user configurations in the order of registration.
- 类型参数:
T
- the type of the bean- 参数:
name
- the bean name ornull
to use a generated nametype
- the type of the beansupplier
- a supplier for the beancustomizers
- one or more callbacks for customizing the factory'sBeanDefinition
, e.g. setting a lazy-init or primary flag- 返回:
- a new instance with the updated bean
-
withUserConfiguration
Register the specified user configuration classes with theApplicationContext
.- 参数:
configurationClasses
- the user configuration classes to add- 返回:
- a new instance with the updated configuration
-
withConfiguration
Register the specified configuration classes with theApplicationContext
.- 参数:
configurations
- the configurations to add- 返回:
- a new instance with the updated configuration
-
with
Apply customization to this runner.- 参数:
customizer
- the customizer to call- 返回:
- a new instance with the customizations applied
-
run
Create and refresh a newApplicationContext
based on the current state of this loader. The context is consumed by the specifiedconsumer
and closed upon completion.- 参数:
consumer
- the consumer of the createdApplicationContext
- 返回:
- this instance
-
prepare
Prepare a newApplicationContext
based on the current state of this loader. The context is consumed by the specifiedconsumer
and closed upon completion. Unlikerun(ContextConsumer)
, this method does not refresh the consumed context.- 参数:
consumer
- the consumer of the createdApplicationContext
- 返回:
- this instance
- 从以下版本开始:
- 3.0.0
-
AbstractApplicationContextRunner(Function, Supplier, Class...)