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) 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?
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?
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>