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 TypeMethodDescriptionvoid
beforeTestExecution(ExtensionContext context)
protected void
configure(ServerBuilder sb)
Configures theServer
with the givenServerBuilder
.protected void
Configures theServerBuilder
beyond the defaults of enabling HTTPs and registering a service for handling enqueued responses.enqueue(AggregatedHttpResponse response)
Enqueues theAggregatedHttpResponse
to return to a client of thisMockWebServerExtension
.enqueue(HttpResponse response)
Enqueues theHttpResponse
to return to a client of thisMockWebServerExtension
.void
reset()
Resets the mocking state of this extension.Returns the nextRecordedRequest
the server received.takeRequest(int amount, TimeUnit unit)
Returns the nextRecordedRequest
the server received.Methods inherited from class com.linecorp.armeria.testing.junit5.server.ServerExtension
after, before, beforeEach, endpoint, hasHttp, hasHttps, httpEndpoint, httpPort, httpsEndpoint, httpSocketAddress, httpsPort, httpsSocketAddress, httpsUri, httpsUri, httpUri, httpUri, port, requestContextCaptor, server, shouldCapture, socketAddress, start, stop, uri, uri
Methods inherited from class com.linecorp.armeria.testing.junit5.common.AbstractAllOrEachExtension
afterAll, afterEach, beforeAll, runForEachTest
-
Constructor Details
-
MockWebServerExtension
public MockWebServerExtension()
-
-
Method Details
-
enqueue
Enqueues theHttpResponse
to return to a client of thisMockWebServerExtension
. Multiple calls will return multiple responses in order. -
enqueue
Enqueues theAggregatedHttpResponse
to return to a client of thisMockWebServerExtension
. Multiple calls will return multiple responses in order. -
takeRequest
Returns the nextRecordedRequest
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
Returns the nextRecordedRequest
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
Description copied from class:ServerExtension
Configures theServer
with the givenServerBuilder
.- Specified by:
configure
in classServerExtension
- Throws:
Exception
-
configureServer
Configures theServerBuilder
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 theservice*
methods, even if you do they will be ignored asMockWebServerExtension
can only serve responses registered withenqueue(HttpResponse)
.- Throws:
Exception
-
beforeTestExecution
- Specified by:
beforeTestExecution
in 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.
-