public class ServiceHelper extends Object implements org.junit.rules.TestRule, Closeable
A JUnit TestRule
for running tests against an apollo service. It is built around
the AppInit
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 a
Rule
annotation. Further configuration like config key overrides, running domain and
additional program arguments can be set up using conf(String, String)
,
domain(String)
and args(String...)
respectively.
Requests can be sent to the running application using any of request(com.spotify.apollo.Request)
methods.
@RunWith(MockitoJUnitRunner.class)
class MyServiceTest {
@Rule
public ServiceHelper serviceHelper = ServiceHelper.create(this::appInit, "my-service")
.conf("some.key", "some-value")
.args("-v")
.startTimeoutSeconds(30);
@Mock
SomeObject someObject;
void appInit(Environment environment) {
// Implements resource "/endpoint" using someObject
RouteProvider endpointResource = new EndpointResource(someObject);
environment.routingEngine()
.registerAutoRoutes(endpointResource);
}
@Test
public void testRequest() throws Exception {
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"));
}
}
@RunWith(MockitoJUnitRunner.class)
class MyServiceTest {
// Implements AppInit
MyService myService = new MyService();
@Rule
public ServiceHelper serviceHelper = ServiceHelper.create(myService, "my-service")
.conf("some.key", "some-value")
.args("-v")
.startTimeoutSeconds(30);
@Test
public void testRequest() throws Exception {
String response = Futures.getUnchecked(serviceHelper.request("GET", "/ping"))
.getPayloads().get(0).toStringUtf8();
assertThat(response, is("pong"));
}
}
The service helper instance will contain a StubClient
that can be accessed
through 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 the
StubClient
. But if none is found the request will be delegated to the underlying
client that is normally available to the application through Environment.client()
or
RequestContext.requestScopedClient()
.
See StubClient
for more docs on how to set up mocked request replies.
Modifier and Type | Method and Description |
---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
ServiceHelper |
args(String... args)
Run the service with the given program arguments.
|
void |
close() |
ServiceHelper |
conf(String key,
Object value)
Run the service with the key/value pair defined in the loaded configuration.
|
ServiceHelper |
conf(String key,
String value)
Run the service with the key/value pair defined in the loaded configuration.
|
static ServiceHelper |
create(AppInit appInit,
String serviceName)
Creates a
ServiceHelper using the given AppInit and service name. |
static ServiceHelper |
create(AppInit appInit,
String serviceName,
StubClient stubClient)
Creates a
ServiceHelper using the given AppInit , service name and
stub client. |
ServiceHelper |
disableMetaApi()
Don't set up
/_meta/* routes for the application. |
ServiceHelper |
domain(String domain)
Run the service in the given domain.
|
ServiceHelper |
forwardingNonStubbedRequests(boolean forward)
Determines whether to forward requests for which nothing has been stubbed.
|
CompletionStage<Response<okio.ByteString>> |
request(Request request)
Make a call to the running application and return a
CompletionStage of the response. |
CompletionStage<Response<okio.ByteString>> |
request(String method,
String uri)
Makes a call on the given uri.
|
CompletionStage<Response<okio.ByteString>> |
request(String method,
String uri,
okio.ByteString payload)
Makes a call on the given uri.
|
CompletionStage<Response<okio.ByteString>> |
request(String method,
URI uri)
Makes a call on the given uri.
|
CompletionStage<Response<okio.ByteString>> |
request(String method,
URI uri,
okio.ByteString payload)
Makes a call on the given uri.
|
ServiceHelper |
resetConf(String key)
Reset a key in the configuration
|
ServiceHelper |
scheme(String scheme)
Set the scheme to be used for relative request uris on this ServiceHelper instance.
|
Client |
serviceClient()
Get a
Client that allows to make requests to the service created by this helper |
void |
start()
Starts the service, blocking for maximum
timeoutSeconds until it has come up. |
ServiceHelper |
startTimeoutSeconds(int timeoutSeconds)
Set the time to wait for the service to start before giving up.
|
StubClient |
stubClient()
A
StubClient that can be used to mock outgoing application request responses. |
ServiceHelper |
withModule(ApolloModule module) |
public static final String[] NO_ARGS
public static ServiceHelper create(AppInit appInit, String serviceName)
ServiceHelper
using the given AppInit
and service name.appInit
- The init function for the test setupserviceName
- The service name for looking up configRule
public static ServiceHelper create(AppInit appInit, String serviceName, StubClient stubClient)
ServiceHelper
using the given AppInit
, service name and
stub client. Use, for instance, when you want to configure the thread count of the stub client.appInit
- The init function for the test setupserviceName
- The service name for looking up configstubClient
- The stub client to useRule
public ServiceHelper domain(String domain)
"apollo.backend"
config key
which is also available through Environment.domain()
.domain
- The domain to usepublic ServiceHelper disableMetaApi()
/_meta/*
routes for the application.public ServiceHelper args(String... args)
args
- The program arguments to usepublic ServiceHelper conf(String key, String value)
key
- The key to definevalue
- The value to associate with the keypublic ServiceHelper conf(String key, Object value)
key
- The key to definevalue
- The value for the configuration. It can be any accepted type as described by
this method's documentation:
ConfigValueFactory.fromAnyRef(
java.lang.Object, java.lang.String)
public ServiceHelper resetConf(String key)
key
- The path to unsetpublic ServiceHelper forwardingNonStubbedRequests(boolean forward)
forward
- whether to enable forwardingpublic ServiceHelper startTimeoutSeconds(int timeoutSeconds)
public ServiceHelper withModule(ApolloModule module)
public ServiceHelper scheme(String scheme)
scheme
- The scheme to be used for relative request uris (without "://")public StubClient stubClient()
StubClient
that can be used to mock outgoing application request responses.public Client serviceClient()
Client
that allows to make requests to the service created by this helperpublic CompletionStage<Response<okio.ByteString>> request(Request request)
CompletionStage
of the response.request
- The request to send to the applicationpublic CompletionStage<Response<okio.ByteString>> request(String method, String uri)
"/ping"
or a full path like "http://method
- The method of the calluri
- The uri of the callpublic CompletionStage<Response<okio.ByteString>> request(String method, URI uri)
"/ping"
or a full path like "http://method
- The method of the calluri
- The uri of the callpublic CompletionStage<Response<okio.ByteString>> request(String method, String uri, okio.ByteString payload)
"/ping"
or a full path like "http://method
- The method of the calluri
- The uri of the callpayload
- A payload bodypublic CompletionStage<Response<okio.ByteString>> request(String method, URI uri, okio.ByteString payload)
"/ping"
or a full path like "http://method
- The method of the calluri
- The uri of the callpayload
- A payload bodypublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void start() throws InterruptedException
timeoutSeconds
until it has come up.InterruptedException
Copyright © 2016. All rights reserved.