Skip to content

Commit 917496b

Browse files
committed
Fix: Update Spring Boot 4.x tests to use WebTestClient.bindToServer()
- Replaced TestRestTemplate with WebTestClient for reactive testing - Added reactor-test dependency for WebTestClient support - Updated test to manually create WebTestClient instance using bindToServer() - Tests now pass with Spring Boot 4.0.6 and Java 21
1 parent c4b3d08 commit 917496b

2 files changed

Lines changed: 46 additions & 47 deletions

File tree

chapter4/knative-springboot/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<artifactId>spring-boot-starter-test</artifactId>
5353
<scope>test</scope>
5454
</dependency>
55+
<dependency>
56+
<groupId>io.projectreactor</groupId>
57+
<artifactId>reactor-test</artifactId>
58+
<scope>test</scope>
59+
</dependency>
5560
</dependencies>
5661
<build>
5762
<pluginManagement>

chapter4/knative-springboot/src/test/java/echo/SpringCloudEventsApplicationTests.java

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,70 @@
99
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.SUBJECT;
1010
import static org.springframework.cloud.function.cloudevent.CloudEventMessageUtils.TYPE;
1111

12-
import java.net.URI;
1312
import java.util.UUID;
1413
import org.junit.jupiter.api.Test;
15-
import org.springframework.beans.factory.annotation.Autowired;
1614
import org.springframework.boot.test.context.SpringBootTest;
1715
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
18-
import org.springframework.boot.test.web.client.TestRestTemplate;
19-
import org.springframework.http.HttpHeaders;
16+
import org.springframework.boot.test.web.server.LocalServerPort;
2017
import org.springframework.http.MediaType;
21-
import org.springframework.http.RequestEntity;
22-
import org.springframework.http.ResponseEntity;
18+
import org.springframework.test.web.reactive.server.WebTestClient;
2319

2420
@SpringBootTest(classes = SpringCloudEventsApplication.class,
2521
webEnvironment = WebEnvironment.RANDOM_PORT)
2622
public class SpringCloudEventsApplicationTests {
2723

28-
@Autowired
29-
private TestRestTemplate rest;
24+
@LocalServerPort
25+
private int port;
26+
27+
private WebTestClient getWebTestClient() {
28+
return WebTestClient.bindToServer()
29+
.baseUrl("http://localhost:" + port)
30+
.build();
31+
}
3032

3133
@Test
3234
public void testEchoInput() throws Exception {
3335

3436
String input ="hello";
3537

36-
HttpHeaders ceHeaders = new HttpHeaders();
37-
ceHeaders.add(SPECVERSION, "1.0");
38-
ceHeaders.add(ID, UUID.randomUUID()
39-
.toString());
40-
ceHeaders.add(TYPE, "echo");
41-
ceHeaders.add(SOURCE, "http://localhost:8080/echo");
42-
ceHeaders.add(SUBJECT, "Echo content");
43-
44-
ResponseEntity<String> response = this.rest.exchange(
45-
RequestEntity.post(new URI("/echo"))
46-
.contentType(MediaType.APPLICATION_JSON)
47-
.headers(ceHeaders)
48-
.body(input),
49-
String.class);
50-
51-
assertThat(response.getStatusCode()
52-
.value(), equalTo(200));
53-
String body = response.getBody();
54-
assertThat(body, notNullValue());
55-
assertThat(body, equalTo(input));
38+
getWebTestClient().post()
39+
.uri("/echo")
40+
.contentType(MediaType.APPLICATION_JSON)
41+
.header(SPECVERSION, "1.0")
42+
.header(ID, UUID.randomUUID().toString())
43+
.header(TYPE, "echo")
44+
.header(SOURCE, "http://localhost:8080/echo")
45+
.header(SUBJECT, "Echo content")
46+
.bodyValue(input)
47+
.exchange()
48+
.expectStatus().isOk()
49+
.expectBody(String.class)
50+
.value(body -> {
51+
assertThat(body, notNullValue());
52+
assertThat(body, equalTo(input));
53+
});
5654
}
5755

5856
@Test
5957
public void testEchoRoutingBasedOnType() throws Exception {
6058

6159
String input ="hello";
6260

63-
HttpHeaders ceHeaders = new HttpHeaders();
64-
ceHeaders.add(SPECVERSION, "1.0");
65-
ceHeaders.add(ID, UUID.randomUUID()
66-
.toString());
67-
ceHeaders.add(TYPE, "echo");
68-
ceHeaders.add(SOURCE, "http://localhost:8080/echo");
69-
ceHeaders.add(SUBJECT, "Echo content");
70-
71-
ResponseEntity<String> response = this.rest.exchange(
72-
RequestEntity.post(new URI("/"))
61+
getWebTestClient().post()
62+
.uri("/")
7363
.contentType(MediaType.APPLICATION_JSON)
74-
.headers(ceHeaders)
75-
.body(input),
76-
String.class);
77-
78-
assertThat(response.getStatusCode()
79-
.value(), equalTo(200));
80-
String body = response.getBody();
81-
assertThat(body, notNullValue());
82-
assertThat(body, equalTo(input));
64+
.header(SPECVERSION, "1.0")
65+
.header(ID, UUID.randomUUID().toString())
66+
.header(TYPE, "echo")
67+
.header(SOURCE, "http://localhost:8080/echo")
68+
.header(SUBJECT, "Echo content")
69+
.bodyValue(input)
70+
.exchange()
71+
.expectStatus().isOk()
72+
.expectBody(String.class)
73+
.value(body -> {
74+
assertThat(body, notNullValue());
75+
assertThat(body, equalTo(input));
76+
});
8377
}
8478
}

0 commit comments

Comments
 (0)