
To make sure our wrapper client maps incoming arguments correctly to the request, we can examine the request body. Verify Input Mapping and Request Serialization Calling the takeRequest() method returns a RecordedRequest, which holds the HTTP method, URL, path, headers, and request body. To verify that we used the correct HTTP method and URL in our request, we can ask MockWebServer to retrieve the last request. Let’s take a look at a TwilioClient wrapper class implementation that tries to send SMS messages using the Twilio public class TwilioClient However, this time, we are making requests and processing responses instead of processing requests and returning responses. When we make calls to other services using WebClient, our code has similar responsibilities. In an earlier article of this mini-series, we explored testing the REST controllers with We saw that the controllers have many responsibilities and it’s not enough to write just unit tests.


It’s the course about Spring Boot testing I would have created had I been inclined (so I have no problem recommending it to you using my affiliate link). If you are interested in a complete course on the topic, check out Testing Spring Boot Applications Masterclass by Philip Riecks. Then, we will look at what kind of tests we should write to test those responsibilities and how. addHeader(contentType) MockWebServer proxy new MockWebServer() proxy.enqueue(response) tBodyDelayTimeMs(timeoutDelay)) ay(10000). In this article, we look at how to write tests for WebClient REST calls.įirst, we will discuss what responsibilities a WebClient has. Once the object is created, you can stub the mock response. private final MockWebServer mockWebServer new MockWebServer() Step 2. There are some other popular alternatives, you can consider, such as WireMock.This article is the fifth part of the Spring Boot Testing mini-series. First, create an object of MockWebServer, like below. We learned to start and stop the server, setup mocks, write success and error tests, verify the details of sent requests etc. Starting and Stopping the MockWebServer 3.

We will be using Spring WebClient as HTTP client to invoke the mocked APIs. In this tutorial, we learned to use MockWebServer to mock APIs and responses and later consume these API using WebClient. In this tutorial, we will learn to setup MockWebServer in JUnit 5 tests. RecordedRequest request = server.takeRequest() ĪssertEquals("/api-url-two", request.getPath()) ĪssertEquals("POST", request.getMethod()) ĪssertNotNull(request.getHeader("Authorization")) ĪssertEquals("", request.getBody().readUtf8()) 6. We can use RecordedRequest instance to fetch the details of HTTP requests MockWebServer to make sure our WebClient sent it correctly. This is especially useful when we are implementing and testing the retry logic. Sometimes it is important to verify how many times a request was hit on the mock server. Calling enqueue () sequentially would put multiple responses in the queue, and return them one by one for each request. When constructing a mock response, we can set the response code, headers, and response body. The given mock will send the response in 5 chunks. To make MockWebServer return canned responses, we can call the enqueue () method that takes a MockResponse as an argument. To test a slow network, we can use setChunkedBody() method to send the response in chunks. setBodyDelay(5000, TimeUnit.MILLISECONDS) MockWebServer supports these kinds of erroneous mock responses.įor example, we can test the timeout logic and delayed responses using setBodyDelay() method.

we may get different error codes and other fails such as network issues and latencies. StepVerifier.create(apiResponse)ĪPI responses will not be successful all the time. Once the API response is available, we can project Reactor’s StepVerifier to test these async responses. Mono apiResponse = webClient.post()īase64Utils.encodeToString(("username:password").getBytes(UTF_8))) create(String.format(" server.getHostName())) įinally, hit the mock API and pass on the request parameters and body, as necessary. To get the API host URL, use server.getHostName() method. Normal JUnit TestsĪfter setting up the mocks, we can hit the mock APIs using Spring WebClient. We can use a different port by specifying in start() method. By default, the server starts in port 8080. The following example uses the and hooks to start and stop the server. stop the server after the end of the tests.We can use the MockWebServer similar to other such libraries, such as WireMock.
