Class MockWebServerExtension

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.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.Extension

public class MockWebServerExtension extends ServerExtension implements org.junit.jupiter.api.extension.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