Class 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 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 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 interface org.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 component
        componentClass - 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 component
        componentInstance - 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)