Class 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 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 RegisterExtension 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. The created ServiceHelper could be accessed from a test method using a getter method getServiceHelper(). This extension also implements ParameterResolver and can inject ServiceHelper 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 via RegisterExtension 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 through ServiceHelper.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.

    See Also:
    ServiceHelper, RegisterExtension, ParameterResolver
    • Method Detail

      • forwardingNonStubbedRequests

        public ServiceHelperExtension forwardingNonStubbedRequests​(boolean forward)
      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                        throws Exception
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        Throws:
        Exception
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        afterEach in interface org.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 interface org.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 interface org.junit.jupiter.api.extension.ParameterResolver
        Throws:
        org.junit.jupiter.api.extension.ParameterResolutionException