-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce an alternative to WebTestClient based on RestClient #31275
Comments
RestClient is using the same underlying infrastructure as RestTemplate. You can use the same testing infrastructure as a result. Would spring-projects/spring-boot#37033 solve the problem for you? |
But correct me if I'm wrong @bclozel, the problem is that the testing infrastructure for This is how I'd call a controller with @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SomeTestIT {
@Autowired private TestRestTemplate rest;
@Test
void someTest() {
var response = rest.getForEntity("/something", Something.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).usingRecursiveComparison().isEqualTo(expectedObject);
}
} There is basically nothing that helps testing the http call itself, all the assertions are manual. This is instead how it's done with @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SomeTestIT {
@Autowired private WebTestClient webTestClient;
@Test
void someTest() {
webTestClient
.get()
.uri("/something")
.exchange()
.expectStatus()
.isOk()
.expectBody(Something.class)
.isEqualTo(expectedObject);
}
} It's just a much more polished and pleasant api to work with. Also it could easily be integrated with spring rest docs to generate some nice documentation, which couldn't be done with Regarding spring-projects/spring-boot#37033 I don't think it is enough because they are discussing about a |
WebTestClient
based on RestClient
@EvaristeGalois11 you probably are aware, but just in case, you can use |
WebTestClient
based on RestClient
You would still need |
|
I was just looking for a test equivalent of Using |
IMHO when you introduce new api (RestClient) it should be "complete". Saying that use RestClient in application and WebTestClient in the test is little bit confusing and not consistent - especially for new users who just coming to Spring. |
I've created a sample application to demonstrate a problem with using
If you start the server before running (2) the test passes, so it seems the reason for the failure is the server is not running. However, it's not necessary to start the server before running (3). If it's possible for the test in (3) to pass without starting the server, then it ought to be possible with (2), because the only obvious difference between these tests is the client library. |
For MVC gateway, I created a TestRestClient. IIRC, I copied WebTestClient and adapted it to RestClient https://github.com/spring-cloud/spring-cloud-gateway/tree/main/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/client |
I'm working on this here. |
Hi, I searched the other issues discussing the introduction of
RestClient
but i couldn't find anything regarding this topic. I apologize if it's been already discussed somewhere.Currently the only way to call a controller in a
RANDOM_PORT
environment is throughTestRestTemplate
(which is a spring boot addition) or with aRestClient
instance built for the task. Both of this approaches aren't very nice, given that one must extract from the response object all the parts and then manually asserting on them.WebClientTest
on the other hand has a really nice api with the assertions baked in, but it needs all the reactive stuff to be brought in.A third party alternative is REST Assured which works quite well and has a very nice api, but it would be better i think to not have to rely on some external libraries for this type of thing.
Could it be possible to add a very similar api to
WebTestClient
(RestTestClient
?) but without the reactive side of it?The text was updated successfully, but these errors were encountered: