Testing spring webclient without spring context

Spring webclient is an important component in the spring framework. Its highly customizable, allows us to do perform all the http operations in a declarative way.

But the check is you cant unit test the spring webclient. To perform assertions you would likely need to use wiremock which takes a lot of time for the tests to initialize and run. This affects the motivation to perform the test driven development.

so i decided to write my own library which allows me to perform assertions on webclient call

public WebClient build() {
WebClient.Builder builder = this.builder
new FakeHttpConnector(requestResponsesList)

if (baseUrl != null) {

return builder.build();

The above snippet of code is from the library. I perform the assertions by writing a custom http connector and a exchange function to perform assertions.

How can i use it ?

it available via maven, add the below to the pom.xml file


Lets say i want to test a post request to www.google.com, i could perform assertions like this

FakeWebClientBuilder fakeWebClientBuilder = FakeWebClientBuilder.useDefaultWebClientBuilder();

FakeRequestResponse fakeRequestResponse = new FakeRequestResponseBuilder()
.withRequestBody(BodyInserters.fromFormData("foo", "bar"))

WebClient client =

assertEquals("test", client.method(HttpMethod.POST)
.uri(uriBuilder -> uriBuilder.path("/foo").build())
.body(BodyInserters.fromFormData("foo", "bar"))


simple isn't it ? No more waiting for wiremock to initialize.

If you find it helpful, you can star this repo on github




Post a Comment