Class ServiceHelperExtension
- java.lang.Object
-
- com.spotify.apollo.test.ServiceHelperExtension
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback,org.junit.jupiter.api.extension.BeforeEachCallback,org.junit.jupiter.api.extension.Extension,org.junit.jupiter.api.extension.ParameterResolver
public final class ServiceHelperExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
A JUnit 5 extension for running tests against an apollo service. It is built around theAppInitsetup mechanism and can be used to start a service configured in a way appropriate for the test scenario.Typical usage would use
create(AppInit, String)together with aRegisterExtensionannotation. Further configuration like config key overrides, running domain and additional program arguments can be set up usingconf(String, String),domain(String)andargs(String...)respectively. The createdServiceHelpercould be accessed from a test method using a getter methodgetServiceHelper(). This extension also implementsParameterResolverand can injectServiceHelperinstances into test methods via parameter injection.Declarative extension registration via
ExtendWithis not supported. An exception will be thrown if this extension is registered declaratively. Only programmatic extension registration viaRegisterExtensionis supported.Requests can be sent to the running application using any of
ServiceHelper.request(com.spotify.apollo.Request)methods.Example usage for testing a route provider
class MyServiceTest { @RegisterExtension static final ServiceHelperExtension serviceHelperExtension = ServiceHelperExtension.create(MyServiceTest::appInit, "my-service") .conf("some.key", "some-value") .args("-v") .startTimeoutSeconds(30); static void appInit(Environment environment) { SomeObject someObject = new SomeObject(); // Implements resource "/endpoint" using someObject RouteProvider endpointResource = new EndpointResource(someObject); environment.routingEngine() .registerAutoRoutes(endpointResource); } @Test void testRequest() throws Exception { // access theServiceHelpervia a getter // see the next example for parameter injection ServiceHelper serviceHelper = serviceHelperExtension.getServiceHelper(); when(someObject.thatDoesThings()).thenReturn("a test string"); String response = Futures.getUnchecked(serviceHelper.request("GET", "/endpoint")) .getPayloads().get(0).toStringUtf8(); assertThat(response, is("a test string")); } }Example usage for system or acceptance tests
class MyServiceTest { // ImplementsAppInitstatic final MyService myService = new MyService(); @RegisterExtension static final ServiceHelperExtension serviceHelperExtension = ServiceHelperExtension.create(myService, "my-service") .conf("some.key", "some-value") .args("-v") .startTimeoutSeconds(30); @Test void testRequest(ServiceHelper serviceHelper) throws Exception { String response = Futures.getUnchecked(serviceHelper.request("GET", "/ping")) .getPayloads().get(0).toStringUtf8(); assertThat(response, is("pong")); } }Faking outgoing request responses
The created service helper instance will contain a
StubClientthat can be accessed throughServiceHelper.stubClient(). This can be used to setup mocked replies on outgoing requests. Requests made by the application will first try to match against requests set up in theStubClient. But if none is found the request will be delegated to the underlying client that is normally available to the application throughEnvironment.client()orRequestContext.requestScopedClient().See
StubClientfor more docs on how to set up mocked request replies.- See Also:
ServiceHelper,RegisterExtension,ParameterResolver
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)ServiceHelperExtensionargs(String... args)voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)ServiceHelperExtensionconf(String key, Object value)ServiceHelperExtensionconf(String key, String value)static ServiceHelperExtensioncreate(com.spotify.apollo.AppInit appInit, String serviceName)static ServiceHelperExtensioncreate(com.spotify.apollo.AppInit appInit, String serviceName, StubClient stubClient)ServiceHelperExtensiondisableMetaApi()ServiceHelperExtensiondomain(String domain)ServiceHelperExtensionforwardingNonStubbedRequests(boolean forward)ServiceHelpergetServiceHelper()ServiceHelperExtensionresetConf(String key)ObjectresolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)ServiceHelperExtensionscheme(String scheme)ServiceHelperExtensionstartTimeoutSeconds(int timeoutSeconds)booleansupportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)ServiceHelperExtensionwithModule(com.spotify.apollo.module.ApolloModule module)
-
-
-
Method Detail
-
create
public static ServiceHelperExtension create(com.spotify.apollo.AppInit appInit, String serviceName)
-
create
public static ServiceHelperExtension create(com.spotify.apollo.AppInit appInit, String serviceName, StubClient stubClient)
-
domain
public ServiceHelperExtension domain(String domain)
-
disableMetaApi
public ServiceHelperExtension disableMetaApi()
-
args
public ServiceHelperExtension args(String... args)
-
conf
public ServiceHelperExtension conf(String key, String value)
-
conf
public ServiceHelperExtension conf(String key, Object value)
-
resetConf
public ServiceHelperExtension resetConf(String key)
-
forwardingNonStubbedRequests
public ServiceHelperExtension forwardingNonStubbedRequests(boolean forward)
-
startTimeoutSeconds
public ServiceHelperExtension startTimeoutSeconds(int timeoutSeconds)
-
withModule
public ServiceHelperExtension withModule(com.spotify.apollo.module.ApolloModule module)
-
scheme
public ServiceHelperExtension scheme(String scheme)
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) throws org.junit.jupiter.api.extension.ParameterResolutionException- Specified by:
supportsParameterin interfaceorg.junit.jupiter.api.extension.ParameterResolver- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
resolveParameter
public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) throws org.junit.jupiter.api.extension.ParameterResolutionException
- Specified by:
resolveParameterin interfaceorg.junit.jupiter.api.extension.ParameterResolver- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
getServiceHelper
public ServiceHelper getServiceHelper()
-
-