Class SpringContextRule
java.lang.Object
com.atlassian.bamboo.testutils.junit.rule.SpringContextRule
- All Implemented Interfaces:
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 Summary
ConstructorDescriptionSpringContextRule
(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier) -
Method Summary
Modifier and TypeMethodDescriptionorg.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 Details
-
SpringContextRule
public SpringContextRule() -
SpringContextRule
public SpringContextRule(Supplier<com.atlassian.spring.container.ContainerContext> containerContextSupplier)
-
-
Method Details
-
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
-
autowireBean
-
getRealBean
-
getRealBean
-