Class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>>    
- Type Parameters:
- SELF- the "self" type for this runner
- C- the context type
- A- the application context assertion provider
- Direct Known Subclasses:
- ApplicationContextRunner,- ReactiveWebApplicationContextRunner,- WebApplicationContextRunner
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
     });
 }
 - Since:
- 2.0.0
- Author:
- Stephane Nicoll, Andy Wilkinson, Phillip Webb
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected static final classA Bean registration to be applied when the context loaded.protected static final class
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedAbstractApplicationContextRunner(Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory, Supplier<C> contextFactory, Class<?>... additionalContextInterfaces) Create a newAbstractApplicationContextRunnerinstance.protectedAbstractApplicationContextRunner(Supplier<C> contextFactory, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Deprecated, for removal: This API element is subject to removal in a future version.protectedAbstractApplicationContextRunner(AbstractApplicationContextRunner.RunnerConfiguration<C> configuration, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Create a newAbstractApplicationContextRunnerinstance.
- 
Method SummaryModifier and TypeMethodDescriptionprepare(ContextConsumer<? super A> consumer) Prepare a newApplicationContextbased on the current state of this loader.run(ContextConsumer<? super A> consumer) Create and refresh a newApplicationContextbased 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> SELFRegister the specified user bean with theApplicationContext.<T> SELFwithBean(Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext.<T> SELFRegister the specified user bean with theApplicationContext.<T> SELFwithBean(String name, Class<T> type, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) Register the specified user bean with theApplicationContext.withClassLoader(ClassLoader classLoader) Customize theClassLoaderthat theApplicationContextshould use for resource loading and bean class loading.withConfiguration(Configurations configurations) Register the specified configuration classes with theApplicationContext.withInitializer(ApplicationContextInitializer<? super C> initializer) Add anApplicationContextInitializerto be called when the context is created.withParent(ApplicationContext parent) Configure theparentof theApplicationContext.withPropertyValues(String... pairs) Add the specifiedEnvironmentproperty pairs.withSystemProperties(String... pairs) Add the specifiedSystemproperty pairs.withUserConfiguration(Class<?>... configurationClasses) Register the specified user configuration classes with theApplicationContext.
- 
Constructor Details- 
AbstractApplicationContextRunner@Deprecated(since="3.4.0", forRemoval=true) protected AbstractApplicationContextRunner(Supplier<C> contextFactory, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Deprecated, for removal: This API element is subject to removal in a future version.since 3.4.0 for removal in 4.0.0 in favor ofAbstractApplicationContextRunner(Function, Supplier, Class...)Create a newAbstractApplicationContextRunnerinstance.- Parameters:
- contextFactory- the factory used to create the actual context
- instanceFactory- the factory used to create new instance of the runner
- Since:
- 2.6.0
 
- 
AbstractApplicationContextRunnerprotected AbstractApplicationContextRunner(Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory, Supplier<C> contextFactory, Class<?>... additionalContextInterfaces) Create a newAbstractApplicationContextRunnerinstance.- Parameters:
- instanceFactory- the factory used to create new instance of the runner
- contextFactory- the factory used to create the actual context
- additionalContextInterfaces- any additional application context interfaces to be added to the application context proxy
- Since:
- 3.4.0
 
- 
AbstractApplicationContextRunnerprotected AbstractApplicationContextRunner(AbstractApplicationContextRunner.RunnerConfiguration<C> configuration, Function<AbstractApplicationContextRunner.RunnerConfiguration<C>, SELF> instanceFactory) Create a newAbstractApplicationContextRunnerinstance.- Parameters:
- configuration- the configuration for the runner to use
- instanceFactory- the factory used to create new instance of the runner
- Since:
- 2.6.0
 
 
- 
- 
Method Details- 
withAllowBeanDefinitionOverridingSpecify if bean definition overriding, by registering a definition with the same name as an existing definition, should be allowed.- Parameters:
- allowBeanDefinitionOverriding- if bean overriding is allowed
- Returns:
- a new instance with the updated bean definition overriding policy
- Since:
- 2.3.0
- See Also:
 
- 
withAllowCircularReferencesSpecify if circular references between beans should be allowed.- Parameters:
- allowCircularReferences- if circular references between beans are allowed
- Returns:
- a new instance with the updated circular references policy
- Since:
- 2.6.0
- See Also:
 
- 
withInitializerAdd anApplicationContextInitializerto be called when the context is created.- Parameters:
- initializer- the initializer to add
- Returns:
- a new instance with the updated initializers
 
- 
withPropertyValuesAdd the specifiedEnvironmentproperty 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:
 
- 
withSystemPropertiesAdd the specifiedSystemproperty pairs. Key-value pairs can be specified with colon (":") or equals ("=") separators. System properties are added before the context isrunand 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:
 
- 
withClassLoaderCustomize theClassLoaderthat theApplicationContextshould use for resource loading and bean class loading.- Parameters:
- classLoader- the classloader to use (or- nullto use the default)
- Returns:
- a new instance with the updated class loader
- See Also:
 
- 
withParentConfigure theparentof theApplicationContext.- Parameters:
- parent- the parent
- Returns:
- a new instance with the updated parent
 
- 
withBeanRegister the specified user bean with theApplicationContext. The bean name is generated from the configuredBeanNameGeneratoron the underlying context.Such beans are registered after regular user configurations in the order of registration. - Type Parameters:
- T- the type of the bean
- Parameters:
- type- the type of the bean
- constructorArgs- 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- nullor empty)
- Returns:
- a new instance with the updated bean
 
- 
withBeanRegister the specified user bean with theApplicationContext.Such beans are registered after regular user configurations in the order of registration. - Type Parameters:
- T- the type of the bean
- Parameters:
- name- the bean name or- nullto use a generated name
- type- the type of the bean
- constructorArgs- 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- nullor empty)
- Returns:
- a new instance with the updated bean
 
- 
withBeanpublic <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 configuredBeanNameGeneratoron the underlying context.Such beans are registered after regular user configurations in the order of registration. - Type Parameters:
- T- the type of the bean
- Parameters:
- type- the type of the bean
- supplier- a supplier for the bean
- customizers- one or more callbacks for customizing the factory's- BeanDefinition, e.g. setting a lazy-init or primary flag
- Returns:
- a new instance with the updated bean
 
- 
withBeanpublic <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 configuredBeanNameGeneratoron the underlying context.Such beans are registered after regular user configurations in the order of registration. - Type Parameters:
- T- the type of the bean
- Parameters:
- name- the bean name or- nullto use a generated name
- type- the type of the bean
- supplier- a supplier for the bean
- customizers- one or more callbacks for customizing the factory's- BeanDefinition, e.g. setting a lazy-init or primary flag
- Returns:
- a new instance with the updated bean
 
- 
withUserConfigurationRegister the specified user configuration classes with theApplicationContext.- Parameters:
- configurationClasses- the user configuration classes to add
- Returns:
- a new instance with the updated configuration
 
- 
withConfigurationRegister the specified configuration classes with theApplicationContext.- Parameters:
- configurations- the configurations to add
- Returns:
- a new instance with the updated configuration
 
- 
withApply customization to this runner.- Parameters:
- customizer- the customizer to call
- Returns:
- a new instance with the customizations applied
 
- 
runCreate and refresh a newApplicationContextbased on the current state of this loader. The context is consumed by the specifiedconsumerand closed upon completion.- Parameters:
- consumer- the consumer of the created- ApplicationContext
- Returns:
- this instance
 
- 
preparePrepare a newApplicationContextbased on the current state of this loader. The context is consumed by the specifiedconsumerand closed upon completion. Unlikerun(ContextConsumer), this method does not refresh the consumed context.- Parameters:
- consumer- the consumer of the created- ApplicationContext
- Returns:
- this instance
- Since:
- 3.0.0
 
 
- 
AbstractApplicationContextRunner(Function, Supplier, Class...)