4 Ways to test WebClient using Mockito, MockWebServer, WebTestClient and WireMockServer

4 Ways to test WebClient using Mockito, MockWebServer, WebTestClient and WireMockServer

learninjava
Dec 05, 2020 - Webservices

WebClient Series - Table of Contents

Part 1 : How to create Spring WebClient CRUD - GET, POST, PUT and DELETE Examples

Part 2 : 4 Ways to test WebClient using Mockito, MockWebServer, WebTestClient and WireMockServer - [This article]

 

Mockito, MockWebServer, WebTestClient and WireMockServer 

In the previous article, we created a Rest API using WebClient. In this article, we are going to Test or Mock the Rest API that we created using multiple different ways. We will also try to generate code coverage reports for each of these approaches.
We will also compare and help you with which approach you can use based on your needs. So sit tight and follow along.

Dependencies

Let's start by adding all the required dependencies for all the 4 approaches. Observe that we have also added the jacoco plugin to verify if our test cases are doing the code coverage.
loading...

WebClient configuration

Create a configuration class and add a bean for the WebClient initialization.
loading...

Way 1: WebClient mocking using Mockito

loading...

Mockito Approach - Analysis

Type the below command at command prompt to run all the 4 CRUD test cases:
mvn clean test -Dtest=SpringBootWebClientApplicationMockitoTests
Navigate to target > site > jacoco and observe that the code coverage is 100%
https://github.com/learninjavagithub/assets/raw/master/articles/webclient-testing-mockito.jpg

Way 2: WebClient mocking using MockWebServer

loading...

MockWebServer Approach - Analysis

Type the below command at command prompt to run all the 4 CRUD test cases:
mvn clean test -Dtest=SpringBootWebClientApplicationMockWebServerTests
Navigate to target > site > jacoco and observe that the code coverage is again 100% similar to Mockito approach.
https://github.com/learninjavagithub/assets/raw/master/articles/webclient-testing-mockito.jpg

Way 3: WebClient testing using WebTestClient

loading...

WebTestClient Approach - Analysis

Type the below command at command prompt to run all the 4 CRUD test cases:
mvn clean test -Dtest=SpringBootWebClientApplicationWebTestClientTests
Yes, this will fail with errors because WebTestClient needs the API to be up and running. Start the application using the below command and run the test cases again:
mvn spring-boot:run
Now, navigate to target > site > jacoco and observe the code coverage. Boom!! the code coverage is 0% for all our CRUD methods.
https://github.com/learninjavagithub/assets/raw/master/articles/webclient-testing-webtestclient.jpg

Way 4: WebClient testing using WireMockServer

loading...

WireMockServer Approach - Analysis

Type the below command at command prompt to run all the 4 CRUD test cases:
mvn clean test -Dtest=SpringBootWebClientApplicationWireMockTests
Now, navigate to target > site > jacoco and observe the code coverage. Again, the code coverage is 0% for all our CRUD methods.
https://github.com/learninjavagithub/assets/raw/master/articles/webclient-testing-webtestclient.jpg

Comparision - Which one should i choose ?

As you can see the Mockito and MockWebServer approaches are the right choices if you want unit testing and code coverage. If you want to do integration testing then go for WebTestClient or WireMockServer.
Also, note that if you choose WebTestClient approach, your API needs to be running. All other approaches can run without your API up and running. Hope this helps in deciding which approach to go with.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by   or  or  below or on the articles on social media.
 
Like us on:
 
 
a