Class MockWebServerExtension

All Implemented Interfaces:
AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback, BeforeTestExecutionCallback, Extension

public class MockWebServerExtension extends ServerExtension implements BeforeTestExecutionCallback
An Extension primarily for testing clients. MockWebServerExtension will start and stop a Server at the beginning and end of a test class. Call enqueue(HttpResponse) as many times as you will make requests within the test. The enqueued responses will be returned in order for each of these requests. Later, call takeRequest() to retrieve requests in the order the server received them to validate the request parameters.

Note, tests in a class using MockWebServerExtension cannot be run concurrently, i.e., do not set Execution to ExecutionMode.CONCURRENT.

For example,



 > class MyTest {
 >
 >   @RegisterExtension
 >   static MockWebServerExtension server = new MockWebServerExtension();
 >
 >   @Test
 >   void checkSomething() {
 >       WebClient client = WebClient.of(server.httpUri());
 >
 >       server.enqueue(AggregatedHttpResponse.of(HttpStatus.OK));
 >       server.enqueue(AggregatedHttpResponse.of(HttpStatus.FORBIDDEN));
 >
 >       assertThat(client.get("/").aggregate().join().status()).isEqualTo(HttpStatus.OK);
 >       assertThat(client.get("/bad").aggregate().join().status()).isEqualTo(HttpStatus.FORBIDDEN);
 >
 >       assertThat(server.takeRequest().path()).isEqualTo("/");
 >       assertThat(server.takeRequest().path()).isEqualTo("/bad");
 >   }
 > }
 
  • Constructor Details

    • MockWebServerExtension

      public MockWebServerExtension()
  • Method Details

    • enqueue

      public final MockWebServerExtension enqueue(com.linecorp.armeria.common.HttpResponse response)
      Enqueues the HttpResponse to return to a client of this MockWebServerExtension. Multiple calls will return multiple responses in order.
    • enqueue

      public final MockWebServerExtension enqueue(com.linecorp.armeria.common.AggregatedHttpResponse response)
      Enqueues the AggregatedHttpResponse to return to a client of this MockWebServerExtension. Multiple calls will return multiple responses in order.
    • takeRequest

      @Nullable public final @Nullable RecordedRequest takeRequest()
      Returns the next RecordedRequest the server received. Call this method multiple times to retrieve the requests, in order. Will block up to 10 seconds waiting for a request.
    • takeRequest

      @Nullable public final @Nullable RecordedRequest takeRequest(int amount, TimeUnit unit)
      Returns the next RecordedRequest the server received. Call this method multiple times to retrieve the requests, in order. Will block the given amount of time waiting for a request.
    • configure

      protected final void configure(com.linecorp.armeria.server.ServerBuilder sb) throws Exception
      Description copied from class: ServerExtension
      Configures the Server with the given ServerBuilder.
      Specified by:
      configure in class ServerExtension
      Throws:
      Exception
    • configureServer

      protected void configureServer(com.linecorp.armeria.server.ServerBuilder sb) throws Exception
      Configures the ServerBuilder beyond the defaults of enabling HTTPs and registering a service for handling enqueued responses. Override this method to configure advanced behavior such as client authentication and timeouts. Don't call any of the service* methods, even if you do they will be ignored as MockWebServerExtension can only serve responses registered with enqueue(HttpResponse).
      Throws:
      Exception
    • beforeTestExecution

      public final void beforeTestExecution(ExtensionContext context)
      Specified by:
      beforeTestExecution in interface BeforeTestExecutionCallback
    • reset

      public void reset()
      Resets the mocking state of this extension. This only needs to be called if using this class without JUnit 5.