AbstractApplicationContextRunner

Utility design to run an 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
    });
}

Author

Stephane Nicoll

Andy Wilkinson

Phillip Webb

Since

2.0.0

Parameters

<SELF>

the "self" type for this runner

<C>

the context type

<A>

the application context assertion provider

See also

Inheritors

Functions

Link copied to clipboard
open fun prepare(consumer: ContextConsumer<in A>): SELF
Prepare a new ApplicationContext based on the current state of this loader.
Link copied to clipboard
open fun run(consumer: ContextConsumer<in A>): SELF
Create and refresh a new ApplicationContext based on the current state of this loader.
Link copied to clipboard
open fun with(customizer: (SELF) -> SELF): SELF
Apply customization to this runner.
Link copied to clipboard
open fun withAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding: Boolean): SELF
Specify if bean definition overriding, by registering a definition with the same name as an existing definition, should be allowed.
Link copied to clipboard
open fun withAllowCircularReferences(allowCircularReferences: Boolean): SELF
Specify if circular references between beans should be allowed.
Link copied to clipboard
open fun <T> withBean(type: Class<T>, constructorArgs: Array<Any>): SELF
open fun <T> withBean(type: Class<T>, supplier: Supplier<T>, customizers: Array<BeanDefinitionCustomizer>): SELF
open fun <T> withBean(name: String, type: Class<T>, constructorArgs: Array<Any>): SELF
open fun <T> withBean(name: String, type: Class<T>, supplier: Supplier<T>, customizers: Array<BeanDefinitionCustomizer>): SELF
Register the specified user bean with the ApplicationContext.
Link copied to clipboard
open fun withClassLoader(classLoader: ClassLoader): SELF
Customize the ClassLoader that the ApplicationContext should use for resource loading and bean class loading.
Link copied to clipboard
open fun withConfiguration(configurations: Configurations): SELF
Register the specified configuration classes with the ApplicationContext.
Link copied to clipboard
Add an ApplicationContextInitializer to be called when the context is created.
Link copied to clipboard
Configure the parent of the ApplicationContext.
Link copied to clipboard
open fun withPropertyValues(pairs: Array<String>): SELF
Add the specified Environment property pairs.
Link copied to clipboard
Add the specified System property pairs.
Link copied to clipboard
open fun withUserConfiguration(configurationClasses: Array<Class<out Any>>): SELF
Register the specified user configuration classes with the ApplicationContext.