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 theAppInit
setup 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 aRegisterExtension
annotation. 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 createdServiceHelper
could be accessed from a test method using a getter methodgetServiceHelper()
. This extension also implementsParameterResolver
and can injectServiceHelper
instances into test methods via parameter injection.Declarative extension registration via
ExtendWith
is not supported. An exception will be thrown if this extension is registered declaratively. Only programmatic extension registration viaRegisterExtension
is 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 the
ServiceHelper
via 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 { // Implements
AppInit
static 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
StubClient
that 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
StubClient
for 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 void
afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
ServiceHelperExtension
args(String... args)
void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
ServiceHelperExtension
conf(String key, Object value)
ServiceHelperExtension
conf(String key, String value)
static ServiceHelperExtension
create(com.spotify.apollo.AppInit appInit, String serviceName)
static ServiceHelperExtension
create(com.spotify.apollo.AppInit appInit, String serviceName, StubClient stubClient)
ServiceHelperExtension
disableMetaApi()
ServiceHelperExtension
domain(String domain)
ServiceHelperExtension
forwardingNonStubbedRequests(boolean forward)
ServiceHelper
getServiceHelper()
ServiceHelperExtension
resetConf(String key)
Object
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)
ServiceHelperExtension
scheme(String scheme)
ServiceHelperExtension
startTimeoutSeconds(int timeoutSeconds)
boolean
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)
ServiceHelperExtension
withModule(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:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
Exception
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
- Specified by:
afterEach
in 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:
supportsParameter
in 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:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
getServiceHelper
public ServiceHelper getServiceHelper()
-
-