Class AstrixRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class AstrixRule
    extends Object
    implements org.junit.rules.TestRule
    Test utility that manages an internal service registry, config-source and AstrixContext. The managed AstrixContext is configured to use the internal service registry.

    The AstrixRule allows registering providers programmatically in the service-registry, see setProxyState(Class, Object). Typical usage:

    
     @ClassRule
     public static AstrixRule astrix = new AstrixRule();
    
     @ClassRule
     public static RunningPu testedPu = ... // setup pu
    
    
     @Test
     public void aTest() {
         ...
         astrix.registerProvider(ConsumedService.class, serviceStub);
         ...
         ServiceUnderTest service = astrix.waitForBean(ServiceUnderTest.class, 2000);
     }
    
     @Test
     public void anotherTest() {
         ...
         astrix.registerProvider(ConsumedService.class, serviceStubWithDifferentBehavior);
         ...
         ServiceUnderTest service = astrix.waitForBean(ServiceUnderTest.class, 2000);
     }
    
     
    Author:
    Elias Lindholm
    • Constructor Detail

      • AstrixRule

        @SafeVarargs
        public AstrixRule​(Class<? extends com.avanza.astrix.test.TestApi>... testApis)
      • AstrixRule

        @SafeVarargs
        public AstrixRule​(com.avanza.astrix.config.MapConfigSource configSource,
                          Class<? extends com.avanza.astrix.test.TestApi>... testApis)
      • AstrixRule

        @SafeVarargs
        public AstrixRule​(Consumer<? super com.avanza.astrix.test.AstrixRuleContext> contextConfigurer,
                          Class<? extends com.avanza.astrix.test.TestApi>... testApis)
      • AstrixRule

        @SafeVarargs
        public AstrixRule​(com.avanza.astrix.config.MapConfigSource configSource,
                          Consumer<? super com.avanza.astrix.test.AstrixRuleContext> contextConfigurer,
                          Class<? extends com.avanza.astrix.test.TestApi>... testApis)
    • 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
      • getConfigSourceId

        public String getConfigSourceId()
        The configSourceId might be used to retrieve the ConfigSource instance using GlobalConfigSourceRegistry.getConfigSource(String)
        Returns:
        the configSourceId for the associated ConfigSource.
      • getConfigSource

        public com.avanza.astrix.config.MapConfigSource getConfigSource()
      • getServiceRegistryUri

        public String getServiceRegistryUri()
        Returns:
        the serviceUri for the associated service-registry.
      • destroy

        public void destroy()
      • registerProxy

        public <T> void registerProxy​(Class<T> service)
        Parameters:
        service - - The qualified bean type to register a proxy for
        See Also:
        AstrixRuleContext.registerProxy(Class)
      • registerProxy

        public <T> void registerProxy​(Class<T> service,
                                      String qualifier)
        Parameters:
        service - - The qualified bean type to register a proxy for
        qualifier - - The qualifier of the bean type to register a proxy for
        See Also:
        AstrixRuleContext.registerProxy(Class)
      • setProxyState

        public <T> void setProxyState​(Class<T> type,
                                      T mock)
        Sets the proxy state for a given proxy in the service registry. If no proxy has bean registered before it will be created with the given initial state, see AstrixRuleContext.registerProxy(Class) for more details. When a service-provider is proxied it allows fast switching of the given provider between different test-runs, without restarting the entire test environment.
        Parameters:
        type - - The api to register a provider for.
        mock - - The instance to delegate all invocations to the given api to. Might be null in which case a ServiceUnavailableException will be thrown when the service is invoked.
      • setProxyState

        public <T> void setProxyState​(Class<T> type,
                                      String qualifier,
                                      T mock)
        Sets the proxy state for a given proxy in the service registry. If no proxy has bean registered before it will be created with the given initial state, see AstrixRuleContext.registerProxy(Class) for more details. When a service-provider is proxied it allows fast switching of the given provider between different test-runs, without restarting the entire test environment.
        Parameters:
        type - - The api to register a provider for.
        qualifier - - The qualifier for the service bean to register a provider for
        mock - - The instance to delegate all invocations to the given api to. Might be null in which case a ServiceUnavailableException will be thrown when the service is invoked.
      • resetProxies

        public void resetProxies()
      • getAstrix

        public com.avanza.astrix.context.Astrix getAstrix()
      • getAstrixContext

        public com.avanza.astrix.context.AstrixContext getAstrixContext()
      • getTestApi

        public <T extends com.avanza.astrix.test.TestApi> T getTestApi​(Class<T> testApi)
      • resetTestApis

        public void resetTestApis()
      • setConfigurationProperty

        public void setConfigurationProperty​(String settingName,
                                             String value)