Class SpringContextRule
- java.lang.Object
-
- com.atlassian.bamboo.testutils.junit.rule.SpringContextRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
@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
orClassRule
), 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 Summary
Constructors Constructor Description SpringContextRule()
SpringContextRule(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
-
-
Constructor Detail
-
SpringContextRule
public SpringContextRule()
-
SpringContextRule
public SpringContextRule(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier)
-
-
Method Detail
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
- Specified by:
apply
in interfaceorg.junit.rules.TestRule
-
mockSpringComponent
public <T> T mockSpringComponent(@NotNull @NotNull String componentKey, @NotNull @NotNull Class<T> componentClass)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.- Parameters:
componentKey
- key of the componentcomponentClass
- class of component
-
mockSpringComponent
public <T> T mockSpringComponent(@NotNull @NotNull String componentKey, @Nullable T componentInstance)
Mock a component that could be accessed by ComponentAccessor or ContainerManager.- Parameters:
componentKey
- key of the componentcomponentInstance
- component itself
-
registerComponentSupplier
public void registerComponentSupplier(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Supplier<?> supplier)
-
autowireBean
public void autowireBean(@NotNull @NotNull Object bean)
-
getRealBean
public <T> T getRealBean(@NotNull @NotNull String name, @NotNull @NotNull Class<T> clazz)
-
getRealBean
public <T> T getRealBean(@NotNull @NotNull Class<T> type)
-
-