Skip to content

Commit 6b3edf8

Browse files
committed
Document AssertJ support for RestTestClient
Closes gh-47881
1 parent 3cc2e9c commit 6b3edf8

File tree

9 files changed

+193
-19
lines changed

9 files changed

+193
-19
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/spring-boot-applications.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ For convenience, tests that need to make REST calls to the started server can ad
183183

184184
include-code::MyRandomPortRestTestClientTests[]
185185

186+
If you prefer to use AssertJ, dedicated assertions are available from javadoc:org.springframework.test.web.servlet.client.assertj.RestTestClientResponse[], as shown in the following example:
187+
188+
include-code::MyRandomPortRestTestClientAssertJTests[]
189+
186190
If you have `spring-webflux` on the classpath, you can also autowire a {url-spring-framework-docs}/testing/webtestclient.html[`WebTestClient`] that provides a similar API:
187191

188192
include-code::MyRandomPortWebTestClientTests[]

documentation/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/springbootapplications/withmockenvironment/MyMockMvcTests.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.test.web.servlet.MockMvc;
2727
import org.springframework.test.web.servlet.assertj.MockMvcTester;
2828
import org.springframework.test.web.servlet.client.RestTestClient;
29+
import org.springframework.test.web.servlet.client.RestTestClient.ResponseSpec;
30+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse;
2931

3032
import static org.assertj.core.api.Assertions.assertThat;
3133
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -39,13 +41,20 @@ class MyMockMvcTests {
3941

4042
@Test
4143
void testWithMockMvc(@Autowired MockMvc mvc) throws Exception {
42-
mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("Hello World"));
44+
// @formatter:off
45+
mvc.perform(get("/"))
46+
.andExpect(status().isOk())
47+
.andExpect(content().string("Hello World"));
48+
// @formatter:on
4349
}
4450

45-
// If AssertJ is on the classpath, you can use MockMvcTester
46-
@Test
51+
@Test // If AssertJ is on the classpath, you can use MockMvcTester
4752
void testWithMockMvcTester(@Autowired MockMvcTester mvc) {
48-
assertThat(mvc.get().uri("/")).hasStatusOk().hasBodyTextEqualTo("Hello World");
53+
// @formatter:off
54+
assertThat(mvc.get().uri("/"))
55+
.hasStatusOk()
56+
.hasBodyTextEqualTo("Hello World");
57+
// @formatter:on
4958
}
5059

5160
@Test
@@ -59,8 +68,17 @@ void testWithRestTestClient(@Autowired RestTestClient webClient) {
5968
// @formatter:on
6069
}
6170

62-
// If Spring WebFlux is on the classpath, you can drive MVC tests with a WebTestClient
63-
@Test
71+
@Test // If you prefer AssertJ, dedicated assertions are available
72+
void testWithRestTestClientAssertJ(@Autowired RestTestClient webClient) {
73+
// @formatter:off
74+
ResponseSpec spec = webClient.get().uri("/").exchange();
75+
RestTestClientResponse response = RestTestClientResponse.from(spec);
76+
assertThat(response).hasStatusOk()
77+
.bodyText().isEqualTo("Hello World");
78+
// @formatter:on
79+
}
80+
81+
@Test // If Spring WebFlux is on the classpath
6482
void testWithWebTestClient(@Autowired WebTestClient webClient) {
6583
// @formatter:off
6684
webClient
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.testing.springbootapplications.withrunningserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
25+
import org.springframework.test.web.servlet.client.RestTestClient;
26+
import org.springframework.test.web.servlet.client.RestTestClient.ResponseSpec;
27+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
32+
@AutoConfigureRestTestClient
33+
class MyRandomPortRestTestClientAssertJTests {
34+
35+
@Test
36+
void exampleTest(@Autowired RestTestClient webClient) {
37+
// @formatter:off
38+
ResponseSpec spec = webClient.get().uri("/").exchange();
39+
RestTestClientResponse response = RestTestClientResponse.from(spec);
40+
assertThat(response).hasStatusOk().bodyText().isEqualTo("Hello World");
41+
// @formatter:on
42+
}
43+
44+
}

documentation/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/springbootapplications/withmockenvironment/MyMockMvcTests.kt

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,36 @@ package org.springframework.boot.docs.testing.springbootapplications.withmockenv
1919
import org.assertj.core.api.Assertions.assertThat
2020
import org.junit.jupiter.api.Test
2121
import org.springframework.beans.factory.annotation.Autowired
22-
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc
2322
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient
2423
import org.springframework.boot.test.context.SpringBootTest
24+
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc
2525
import org.springframework.test.web.reactive.server.WebTestClient
2626
import org.springframework.test.web.reactive.server.expectBody
27+
import org.springframework.test.web.servlet.MockMvc
2728
import org.springframework.test.web.servlet.assertj.MockMvcTester
2829
import org.springframework.test.web.servlet.client.RestTestClient
30+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse
2931
import org.springframework.test.web.servlet.client.expectBody
32+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
33+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
34+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
3035

3136
@SpringBootTest
3237
@AutoConfigureMockMvc
3338
@AutoConfigureRestTestClient
3439
class MyMockMvcTests {
3540

3641
@Test
37-
fun testWithMockMvc(@Autowired mvc: MockMvcTester) {
42+
fun testWithMockMvc(@Autowired mvc: MockMvc) {
43+
mvc.perform(get("/"))
44+
.andExpect(status().isOk())
45+
.andExpect(content().string("Hello World"))
46+
}
47+
48+
@Test // If AssertJ is on the classpath, you can use MockMvcTester
49+
fun testWithMockMvcTester(@Autowired mvc: MockMvcTester) {
3850
assertThat(mvc.get().uri("/")).hasStatusOk()
39-
.hasBodyTextEqualTo("Hello World")
51+
.hasBodyTextEqualTo("Hello World")
4052
}
4153

4254
@Test
@@ -48,16 +60,21 @@ class MyMockMvcTests {
4860
.expectBody<String>().isEqualTo("Hello World")
4961
}
5062

51-
// If Spring WebFlux is on the classpath, you can drive MVC tests with a WebTestClient
63+
@Test // If you prefer AssertJ, dedicated assertions are available
64+
fun testWithRestTestClientAssertJ(@Autowired webClient: RestTestClient) {
65+
val spec = webClient.get().uri("/").exchange()
66+
val response = RestTestClientResponse.from(spec)
67+
assertThat(response).hasStatusOk().bodyText().isEqualTo("Hello World")
68+
}
5269

53-
@Test
70+
71+
@Test // If Spring WebFlux is on the classpath
5472
fun testWithWebTestClient(@Autowired webClient: WebTestClient) {
5573
webClient
56-
.get().uri("/")
57-
.exchange()
58-
.expectStatus().isOk
59-
.expectBody<String>().isEqualTo("Hello World")
74+
.get().uri("/")
75+
.exchange()
76+
.expectStatus().isOk
77+
.expectBody<String>().isEqualTo("Hello World")
6078
}
6179

6280
}
63-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.testing.springbootapplications.withrunningserver
18+
19+
import org.assertj.core.api.Assertions.assertThat
20+
import org.junit.jupiter.api.Test
21+
import org.springframework.beans.factory.annotation.Autowired
22+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient
23+
import org.springframework.boot.test.context.SpringBootTest
24+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
25+
import org.springframework.test.web.servlet.client.RestTestClient
26+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse
27+
28+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
29+
@AutoConfigureRestTestClient
30+
class MyRandomPortRestTestClientAssertJTests {
31+
32+
@Test
33+
fun exampleTest(@Autowired webClient: RestTestClient) {
34+
val exchange = webClient.get().uri("/").exchange()
35+
val response = RestTestClientResponse.from(exchange)
36+
assertThat(response).hasStatusOk()
37+
.bodyText().isEqualTo("Hello World")
38+
}
39+
40+
}
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.testing.springbootapplications.withrunningserver
18+
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.beans.factory.annotation.Autowired
21+
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient
22+
import org.springframework.boot.test.context.SpringBootTest
23+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
24+
import org.springframework.test.web.servlet.client.RestTestClient
25+
import org.springframework.test.web.servlet.client.expectBody
26+
27+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
28+
@AutoConfigureRestTestClient
29+
class MyRandomPortRestTestClientTests {
30+
31+
@Test
32+
fun exampleTest(@Autowired webClient: RestTestClient) {
33+
webClient
34+
.get().uri("/")
35+
.exchange()
36+
.expectStatus().isOk
37+
.expectBody<String>().isEqualTo("Hello World")
38+
}
39+
40+
}
41+

integration-test/spring-boot-test-integration-tests/src/test/java/org/springframework/boot/web/server/test/AbstractSpringBootTestWebServerWebEnvironmentTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
3434
import org.springframework.test.web.servlet.client.RestTestClient;
35+
import org.springframework.test.web.servlet.client.RestTestClient.ResponseSpec;
36+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse;
3537
import org.springframework.web.bind.annotation.RequestMapping;
3638
import org.springframework.web.client.RestTemplate;
3739
import org.springframework.web.context.WebApplicationContext;
@@ -92,7 +94,8 @@ void injectTestRestTemplate() {
9294

9395
@Test
9496
void injectRestTestClient() {
95-
this.restClient.get().uri("/").exchange().expectBody(String.class).isEqualTo("Hello World");
97+
ResponseSpec spec = this.restClient.get().uri("/").exchange();
98+
assertThat(RestTestClientResponse.from(spec)).bodyText().isEqualTo("Hello World");
9699
}
97100

98101
@Test

module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/MockMvcSpringBootTestIntegrationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.test.context.bean.override.mockito.MockitoBean;
3131
import org.springframework.test.web.servlet.MockMvc;
3232
import org.springframework.test.web.servlet.client.RestTestClient;
33+
import org.springframework.test.web.servlet.client.RestTestClient.ResponseSpec;
34+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse;
3335

3436
import static org.assertj.core.api.Assertions.assertThat;
3537
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -83,7 +85,8 @@ void shouldHaveRealService() {
8385

8486
@Test
8587
void shouldTestWithRestTestClient(@Autowired RestTestClient restTestClient) {
86-
restTestClient.get().uri("/one").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("one");
88+
ResponseSpec spec = restTestClient.get().uri("/one").exchange();
89+
assertThat(RestTestClientResponse.from(spec)).hasStatusOk().bodyText().isEqualTo("one");
8790
}
8891

8992
@Test

module/spring-boot-webmvc-test/src/test/java/org/springframework/boot/webmvc/test/autoconfigure/mockmvc/MockMvcTesterSpringBootTestIntegrationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.test.context.bean.override.mockito.MockitoBean;
3131
import org.springframework.test.web.servlet.assertj.MockMvcTester;
3232
import org.springframework.test.web.servlet.client.RestTestClient;
33+
import org.springframework.test.web.servlet.client.RestTestClient.ResponseSpec;
34+
import org.springframework.test.web.servlet.client.assertj.RestTestClientResponse;
3335

3436
import static org.assertj.core.api.Assertions.assertThat;
3537

@@ -79,7 +81,8 @@ void shouldHaveRealService() {
7981

8082
@Test
8183
void shouldTestWithRestTestClient(@Autowired RestTestClient restTestClient) {
82-
restTestClient.get().uri("/one").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("one");
84+
ResponseSpec spec = restTestClient.get().uri("/one").exchange();
85+
assertThat(RestTestClientResponse.from(spec)).hasStatusOk().bodyText().isEqualTo("one");
8386
}
8487

8588
@Test

0 commit comments

Comments
 (0)