Skip to content

Commit f578287

Browse files
committed
Polishing in RestTestClient tests
See gh-34428
1 parent 88ddc9d commit f578287

12 files changed

+36
-19
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.test.web.servlet.client.samples;
17+
package org.springframework.test.web.servlet.client;
1818

1919
import java.net.URI;
2020
import java.nio.charset.StandardCharsets;
@@ -36,7 +36,6 @@
3636
import org.springframework.http.HttpHeaders;
3737
import org.springframework.http.HttpMethod;
3838
import org.springframework.http.MediaType;
39-
import org.springframework.test.web.servlet.client.RestTestClient;
4039
import org.springframework.web.bind.annotation.RequestHeader;
4140
import org.springframework.web.bind.annotation.RequestMapping;
4241
import org.springframework.web.bind.annotation.RestController;
@@ -51,11 +50,13 @@ class RestTestClientTests {
5150

5251
private RestTestClient client;
5352

53+
5454
@BeforeEach
5555
void setUp() {
5656
this.client = RestTestClient.bindToController(new TestController()).build();
5757
}
5858

59+
5960
@Nested
6061
class HttpMethods {
6162

@@ -127,6 +128,7 @@ void testOptions() {
127128

128129
}
129130

131+
130132
@Nested
131133
class Mutation {
132134

@@ -149,6 +151,7 @@ void test() {
149151
}
150152
}
151153

154+
152155
@Nested
153156
class Uris {
154157

@@ -193,6 +196,7 @@ void testURI() {
193196
}
194197
}
195198

199+
196200
@Nested
197201
class Cookies {
198202
@Test
@@ -214,6 +218,7 @@ void testCookies() {
214218
}
215219
}
216220

221+
217222
@Nested
218223
class Headers {
219224
@Test
@@ -271,6 +276,7 @@ void testIfNoneMatch() {
271276
}
272277
}
273278

279+
274280
@Nested
275281
class Expectations {
276282
@Test
@@ -281,6 +287,7 @@ void testExpectCookie() {
281287
}
282288
}
283289

290+
284291
@Nested
285292
class ReturnResults {
286293
@Test
@@ -312,6 +319,7 @@ void testReturnResultParameterizedTypeReference() {
312319
}
313320
}
314321

322+
315323
@RestController
316324
static class TestController {
317325

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ApiVersionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private Map<String, String> performRequest(
9494

9595

9696
@RestController
97-
static class TestController {
97+
private static class TestController {
9898

9999
private static final String HEADER = "X-API-Version";
100100

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ErrorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ void serverException() {
4747
.expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
4848
}
4949

50+
5051
@RestController
51-
static class TestController {
52+
private static class TestController {
5253

5354
@GetMapping("/server-error")
5455
void handleAndThrowException() {

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/HeaderAndCookieTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class HeaderAndCookieTests {
3636

3737
private final RestTestClient client = RestTestClient.bindToController(new TestController()).build();
3838

39+
3940
@Test
4041
void requestResponseHeaderPair() {
4142
this.client.get().uri("/header-echo")
@@ -61,8 +62,9 @@ void setCookies() {
6162
.expectHeader().valueMatches("Set-Cookie", "k1=v1");
6263
}
6364

65+
6466
@RestController
65-
static class TestController {
67+
private static class TestController {
6668

6769
@GetMapping("header-echo")
6870
ResponseEntity<Void> handleHeader(@RequestHeader("h1") String myHeader) {

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/JsonContentTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void postJsonContent() {
129129

130130
@RestController
131131
@RequestMapping("/persons")
132-
static class PersonController {
132+
private static class PersonController {
133133

134134
@GetMapping
135135
List<Person> getPersons() {
@@ -143,11 +143,13 @@ Person getPerson(@PathVariable String firstName, @PathVariable String lastName)
143143

144144
@PostMapping
145145
ResponseEntity<String> savePerson(@RequestBody Person person) {
146-
return ResponseEntity.created(URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()))).build();
146+
URI location = URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()));
147+
return ResponseEntity.created(location).build();
147148
}
148149
}
149150

150-
static class Person {
151+
152+
private static class Person {
151153
private String firstName;
152154
private String lastName;
153155

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ResponseEntityTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
* @author Rob Worsnop
4444
*/
4545
class ResponseEntityTests {
46+
4647
private final RestTestClient client = RestTestClient.bindToController(new PersonController())
4748
.baseUrl("/persons")
4849
.build();
4950

51+
5052
@Test
5153
void entity() {
5254
this.client.get().uri("/John")
@@ -126,7 +128,7 @@ void postEntity() {
126128

127129
@RestController
128130
@RequestMapping("/persons")
129-
static class PersonController {
131+
private static class PersonController {
130132

131133
@GetMapping(path = "/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
132134
Person getPerson(@PathVariable String name) {

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/SoftAssertionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Multiple Exceptions (2):
6161

6262

6363
@RestController
64-
static class TestController {
64+
private static class TestController {
6565

6666
@GetMapping("/test")
6767
String handle() {

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/XmlContentTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ public List<Person> getpersons() {
172172
}
173173
}
174174

175+
175176
@RestController
176177
@RequestMapping("/persons")
177-
static class PersonController {
178+
private static class PersonController {
178179

179180
@GetMapping(produces = MediaType.APPLICATION_XML_VALUE)
180181
PersonsWrapper getPersons() {

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/ApplicationContextTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@
3737
class ApplicationContextTests {
3838

3939
private RestTestClient client;
40+
4041
private final WebApplicationContext context;
4142

43+
4244
public ApplicationContextTests(WebApplicationContext context) {
4345
this.context = context;
4446
}
4547

48+
4649
@BeforeEach
4750
void setUp() {
4851
this.client = RestTestClient.bindToApplicationContext(context).build();
4952
}
5053

54+
5155
@Test
5256
void test() {
5357
this.client.get().uri("/test")

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/FilterTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import jakarta.servlet.Filter;
2323
import jakarta.servlet.FilterChain;
24-
import jakarta.servlet.ServletException;
2524
import jakarta.servlet.http.HttpFilter;
2625
import jakarta.servlet.http.HttpServletRequest;
2726
import jakarta.servlet.http.HttpServletResponse;
@@ -44,7 +43,7 @@ void filter() {
4443

4544
Filter filter = new HttpFilter() {
4645
@Override
47-
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
46+
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException {
4847
res.getWriter().write("It works!");
4948
}
5049
};

0 commit comments

Comments
 (0)