@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(Object bean) |
<T> T |
getRealBean(Class<T> type) |
<T> T |
getRealBean(String name,
Class<T> clazz) |
<T> T |
mockSpringComponent(String componentKey,
Class<T> componentClass)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.
|
<T> T |
mockSpringComponent(String componentKey,
T componentInstance)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.
|
void |
registerComponentSupplier(Class<?> clazz,
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 String componentKey, @NotNull Class<T> componentClass)
componentKey
- key of the componentcomponentClass
- class of componentpublic <T> T mockSpringComponent(@NotNull String componentKey, @Nullable T componentInstance)
componentKey
- key of the componentcomponentInstance
- component itselfpublic void registerComponentSupplier(@NotNull Class<?> clazz, @NotNull Supplier<?> supplier)
public void autowireBean(@NotNull Object bean)
public <T> T getRealBean(@NotNull Class<T> type)
Copyright © 2019 Atlassian Software Systems Pty Ltd. All rights reserved.