Class MockWebServerExtension
java.lang.Object
com.linecorp.armeria.testing.junit5.common.AbstractAllOrEachExtension
com.linecorp.armeria.testing.junit5.server.ServerExtension
com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension
- All Implemented Interfaces:
AfterAllCallback,AfterEachCallback,BeforeAllCallback,BeforeEachCallback,BeforeTestExecutionCallback,Extension
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidbeforeTestExecution(ExtensionContext context) protected final voidconfigure(com.linecorp.armeria.server.ServerBuilder sb) Configures theServerwith the givenServerBuilder.protected voidconfigureServer(com.linecorp.armeria.server.ServerBuilder sb) Configures theServerBuilderbeyond the defaults of enabling HTTPs and registering a service for handling enqueued responses.final MockWebServerExtensionenqueue(com.linecorp.armeria.common.AggregatedHttpResponse response) Enqueues theAggregatedHttpResponseto return to a client of thisMockWebServerExtension.final MockWebServerExtensionenqueue(com.linecorp.armeria.common.HttpResponse response) Enqueues theHttpResponseto return to a client of thisMockWebServerExtension.voidreset()Resets the mocking state of this extension.final @Nullable RecordedRequestReturns the nextRecordedRequestthe server received.final @Nullable RecordedRequesttakeRequest(int amount, TimeUnit unit) Returns the nextRecordedRequestthe server received.Methods inherited from class com.linecorp.armeria.testing.junit5.server.ServerExtension
after, before, beforeEach, blockingWebClient, blockingWebClient, configureWebClient, endpoint, hasHttp, hasHttps, httpEndpoint, httpPort, httpsEndpoint, httpSocketAddress, httpsPort, httpsSocketAddress, httpsUri, httpsUri, httpUri, httpUri, port, requestContextCaptor, restClient, restClient, server, shouldCapture, socketAddress, start, stop, uri, uri, webClient, webClientMethods inherited from class com.linecorp.armeria.testing.junit5.common.AbstractAllOrEachExtension
afterAll, afterEach, beforeAll, runForEachTest
-
Constructor Details
-
MockWebServerExtension
public MockWebServerExtension()
-
-
Method Details
-
enqueue
Enqueues theHttpResponseto return to a client of thisMockWebServerExtension. Multiple calls will return multiple responses in order. -
enqueue
public final MockWebServerExtension enqueue(com.linecorp.armeria.common.AggregatedHttpResponse response) Enqueues theAggregatedHttpResponseto return to a client of thisMockWebServerExtension. Multiple calls will return multiple responses in order. -
takeRequest
Returns the nextRecordedRequestthe server received. Call this method multiple times to retrieve the requests, in order. Will block up to 10 seconds waiting for a request. -
takeRequest
Returns the nextRecordedRequestthe 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
Description copied from class:ServerExtensionConfigures theServerwith the givenServerBuilder.- Specified by:
configurein classServerExtension- Throws:
Exception
-
configureServer
Configures theServerBuilderbeyond 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 theservice*methods, even if you do they will be ignored asMockWebServerExtensioncan only serve responses registered withenqueue(HttpResponse).- Throws:
Exception
-
beforeTestExecution
- Specified by:
beforeTestExecutionin interfaceBeforeTestExecutionCallback
-
reset
public void reset()Resets the mocking state of this extension. This only needs to be called if using this class without JUnit 5.
-