Correct dependencies to use when using WebClient with Spring MVC
Asked Answered
S

1

7

I'm using Spring MVC to develop some controllers.

I would like to write some scenario integration tests which will involve calling multiple controllers of my application.

Normally I would have just used RestTemplate within these tests but the documentation states:

Please, consider using the org.springframework.web.reactive.client.WebClient which has a more modern API and supports sync, async, and streaming scenarios.

And so I would like to write future-proof code by using the WebClient. But I have a number of questions:

  1. What dependencies should be included here?
 // (1) For Spring MVC:
 'org.springframework.boot:spring-boot-starter-web:2.4.1'
 // (2) For spring webflux
 'org.springframework.boot:spring-boot-starter-webflux:2.4.1'

The problem is am I not including too much by having both (1) and (2)? Is there a separate dependency which I should include specifically just to get access to the WebClient? Or is it best practice to include both of these?

  1. Should I write the tests using the WebClient or TestWebClient?

Given that I just want to make HTTP requests to my server in the integration tests I write - I think using the WebClient is fine? Or is it preferred to use the TestWebClient within these sort of tests? What is best practice?

Softa answered 6/1, 2021 at 14:14 Comment(1)
WebTestClient contains request methods that are similar to WebClient. In addition, it contains methods to check the response status, header and body. You can also use assertion libraries like AssertJ with WebTestClient. 34codefactory.medium.com/…Instrumentalist
I
3

Tested converting an existing integration test that was using TestRestTemplate into integration test using WebTestClient

package no.mycompany.myapp.user;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginControllerTest {

    @Autowired
    WebTestClient webTestClient;
        
    @Test
    public void postLogin_withoutUserCredentials_receiveUnauthorized() {
        webTestClient.post()
                .uri("/login")
                .exchange()
                .expectStatus().isUnauthorized();
    }
}

Added this to POM

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <scope>test</scope>
    </dependency>

The following was already in POM

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
Instrumentalist answered 6/1, 2021 at 15:48 Comment(2)
Thanks - this works great. I guess using the WebClient alonside MVC requires the whole webflux starter.Softa
@vab2048: Good to hear. And yes, just remove <scope>test</scope> from my answer. BRInstrumentalist

© 2022 - 2024 — McMap. All rights reserved.