@ThreadSafe public class SpringContextRule extends Object implements org.junit.rules.TestRule
A JUnit rule to be used when Spring context mocking is required.
The rule will preserve Spring container from before the test method or test class execution (depending on whether
the rule is used as a Rule
or ClassRule
), and restore it after execution. To ensure thread safety
synchronization will be used.
The rule also provides methods for mocking the container context and specific Spring components.
Constructor and Description |
---|
SpringContextRule() |
SpringContextRule(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier) |
Modifier and Type | Method and Description |
---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
void |
autowireBean(@NotNull Object bean) |
<T> T |
getRealBean(@NotNull Class<T> type) |
<T> T |
getRealBean(@NotNull String name,
@NotNull Class<T> clazz) |
<T> T |
mockSpringComponent(@NotNull String componentKey,
@NotNull Class<T> componentClass)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.
|
<T> T |
mockSpringComponent(@NotNull String componentKey,
T componentInstance)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.
|
void |
registerComponentSupplier(@NotNull Class<?> clazz,
@NotNull Supplier<?> supplier) |
public SpringContextRule()
public SpringContextRule(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier)
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
public <T> T mockSpringComponent(@NotNull @NotNull String componentKey, @NotNull @NotNull Class<T> componentClass)
componentKey
- key of the componentcomponentClass
- class of componentpublic <T> T mockSpringComponent(@NotNull @NotNull String componentKey, @Nullable T componentInstance)
componentKey
- key of the componentcomponentInstance
- component itselfpublic void registerComponentSupplier(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Supplier<?> supplier)
public void autowireBean(@NotNull @NotNull Object bean)
public <T> T getRealBean(@NotNull @NotNull String name, @NotNull @NotNull Class<T> clazz)
public <T> T getRealBean(@NotNull @NotNull Class<T> type)
Copyright © 2022 Atlassian Software Systems Pty Ltd. All rights reserved.