Package com.avanza.astrix.test
Class AstrixRule
- java.lang.Object
-
- com.avanza.astrix.test.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 Summary
Constructors Constructor Description AstrixRule(com.avanza.astrix.config.MapConfigSource configSource, Class<? extends com.avanza.astrix.test.TestApi>... testApis)AstrixRule(com.avanza.astrix.config.MapConfigSource configSource, Consumer<? super com.avanza.astrix.test.AstrixRuleContext> contextConfigurer, Class<? extends com.avanza.astrix.test.TestApi>... testApis)AstrixRule(Class<? extends com.avanza.astrix.test.TestApi>... testApis)AstrixRule(Consumer<? super com.avanza.astrix.test.AstrixRuleContext> contextConfigurer, Class<? extends com.avanza.astrix.test.TestApi>... testApis)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)voiddestroy()com.avanza.astrix.context.AstrixgetAstrix()com.avanza.astrix.context.AstrixContextgetAstrixContext()com.avanza.astrix.config.MapConfigSourcegetConfigSource()StringgetConfigSourceId()The configSourceId might be used to retrieve the ConfigSource instance usingGlobalConfigSourceRegistry.getConfigSource(String)StringgetServiceRegistryUri()<T extends com.avanza.astrix.test.TestApi>
TgetTestApi(Class<T> testApi)<T> voidregisterProxy(Class<T> service)<T> voidregisterProxy(Class<T> service, String qualifier)voidresetProxies()voidresetTestApis()voidsetConfigurationProperty(String settingName, String value)<T> voidsetProxyState(Class<T> type, String qualifier, T mock)Sets the proxy state for a given proxy in the service registry.<T> voidsetProxyState(Class<T> type, T mock)Sets the proxy state for a given proxy in the service registry.<T> TwaitForBean(Class<T> type, long timeoutMillis)<T> TwaitForBean(Class<T> type, String qualifier, long timeoutMillis)
-
-
-
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:
applyin interfaceorg.junit.rules.TestRule
-
getConfigSourceId
public String getConfigSourceId()
The configSourceId might be used to retrieve the ConfigSource instance usingGlobalConfigSourceRegistry.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 forqualifier- - 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, seeAstrixRuleContext.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, seeAstrixRuleContext.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 formock- - 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()
-
waitForBean
public <T> T waitForBean(Class<T> type, long timeoutMillis) throws InterruptedException
- Throws:
InterruptedException
-
waitForBean
public <T> T waitForBean(Class<T> type, String qualifier, long timeoutMillis) throws InterruptedException
- Throws:
InterruptedException
-
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()
-
-