Class WebServerExtension

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstancePostProcessor

    public class WebServerExtension
    extends Object
    implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver

    Extension for JUnit to create MockResponse through annotations.

    The following is an example on the simplest usage of this extention:
    
     public class MyTest {
    
          // Register the extension, and retain a reference to it if you need any data from it.
         @RegisterExtension
          public static final WebServerExtension serverExtension = new WebServerExtension();
    
         @WebServerTest("api_response.json")
          public void testMyApi() {
              MyApi api = new MyApi(serverExtension.getRequestUrl());
              MyResponseObject response = myApi.makeApiRequest();
          }
      }
    Since:
    1.0.0
    • Method Detail

      • beforeAll

        public void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
        Throws:
        Exception
      • afterAll

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

        public void postProcessTestInstance​(Object testInstance,
                                            org.junit.jupiter.api.extension.ExtensionContext context)
                                     throws Exception
        Specified by:
        postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
        Throws:
        Exception
      • supportsParameter

        public boolean supportsParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext,
                                         org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      • resolveParameter

        public Object resolveParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext,
                                       org.junit.jupiter.api.extension.ExtensionContext extensionContext)
        Specified by:
        resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      • getRequestUrl

        public URL getRequestUrl()
        Returns:
        The root address of the mock webserver.
      • getRequestUrl

        public URL getRequestUrl​(String path)
        Parameters:
        path - The path relative of the server root location.
        Returns:
        The address of the server to the specific path.
      • getServer

        public okhttp3.mockwebserver.MockWebServer getServer()
        Returns:
        The undelying mock webserver instance.