mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-24 15:49:26 +00:00
Polishing in RestTestClient tests
See gh-34428
This commit is contained in:
+10
-2
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.web.servlet.client.samples;
|
||||
package org.springframework.test.web.servlet.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -36,7 +36,6 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.client.RestTestClient;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -51,11 +50,13 @@ class RestTestClientTests {
|
||||
|
||||
private RestTestClient client;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.client = RestTestClient.bindToController(new TestController()).build();
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class HttpMethods {
|
||||
|
||||
@@ -127,6 +128,7 @@ class RestTestClientTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class Mutation {
|
||||
|
||||
@@ -149,6 +151,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class Uris {
|
||||
|
||||
@@ -193,6 +196,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class Cookies {
|
||||
@Test
|
||||
@@ -214,6 +218,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class Headers {
|
||||
@Test
|
||||
@@ -271,6 +276,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class Expectations {
|
||||
@Test
|
||||
@@ -281,6 +287,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
class ReturnResults {
|
||||
@Test
|
||||
@@ -312,6 +319,7 @@ class RestTestClientTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ public class ApiVersionTests {
|
||||
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
private static class TestController {
|
||||
|
||||
private static final String HEADER = "X-API-Version";
|
||||
|
||||
|
||||
+2
-1
@@ -47,8 +47,9 @@ class ErrorTests {
|
||||
.expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
private static class TestController {
|
||||
|
||||
@GetMapping("/server-error")
|
||||
void handleAndThrowException() {
|
||||
|
||||
+3
-1
@@ -36,6 +36,7 @@ class HeaderAndCookieTests {
|
||||
|
||||
private final RestTestClient client = RestTestClient.bindToController(new TestController()).build();
|
||||
|
||||
|
||||
@Test
|
||||
void requestResponseHeaderPair() {
|
||||
this.client.get().uri("/header-echo")
|
||||
@@ -61,8 +62,9 @@ class HeaderAndCookieTests {
|
||||
.expectHeader().valueMatches("Set-Cookie", "k1=v1");
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
private static class TestController {
|
||||
|
||||
@GetMapping("header-echo")
|
||||
ResponseEntity<Void> handleHeader(@RequestHeader("h1") String myHeader) {
|
||||
|
||||
+5
-3
@@ -129,7 +129,7 @@ class JsonContentTests {
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/persons")
|
||||
static class PersonController {
|
||||
private static class PersonController {
|
||||
|
||||
@GetMapping
|
||||
List<Person> getPersons() {
|
||||
@@ -143,11 +143,13 @@ class JsonContentTests {
|
||||
|
||||
@PostMapping
|
||||
ResponseEntity<String> savePerson(@RequestBody Person person) {
|
||||
return ResponseEntity.created(URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()))).build();
|
||||
URI location = URI.create(String.format("/persons/%s/%s", person.getFirstName(), person.getLastName()));
|
||||
return ResponseEntity.created(location).build();
|
||||
}
|
||||
}
|
||||
|
||||
static class Person {
|
||||
|
||||
private static class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
|
||||
+3
-1
@@ -43,10 +43,12 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
* @author Rob Worsnop
|
||||
*/
|
||||
class ResponseEntityTests {
|
||||
|
||||
private final RestTestClient client = RestTestClient.bindToController(new PersonController())
|
||||
.baseUrl("/persons")
|
||||
.build();
|
||||
|
||||
|
||||
@Test
|
||||
void entity() {
|
||||
this.client.get().uri("/John")
|
||||
@@ -126,7 +128,7 @@ class ResponseEntityTests {
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/persons")
|
||||
static class PersonController {
|
||||
private static class PersonController {
|
||||
|
||||
@GetMapping(path = "/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
Person getPerson(@PathVariable String name) {
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ class SoftAssertionTests {
|
||||
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
private static class TestController {
|
||||
|
||||
@GetMapping("/test")
|
||||
String handle() {
|
||||
|
||||
+2
-1
@@ -172,9 +172,10 @@ class XmlContentTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/persons")
|
||||
static class PersonController {
|
||||
private static class PersonController {
|
||||
|
||||
@GetMapping(produces = MediaType.APPLICATION_XML_VALUE)
|
||||
PersonsWrapper getPersons() {
|
||||
|
||||
+4
@@ -37,17 +37,21 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
class ApplicationContextTests {
|
||||
|
||||
private RestTestClient client;
|
||||
|
||||
private final WebApplicationContext context;
|
||||
|
||||
|
||||
public ApplicationContextTests(WebApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.client = RestTestClient.bindToApplicationContext(context).build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
this.client.get().uri("/test")
|
||||
|
||||
+1
-2
@@ -21,7 +21,6 @@ import java.util.Optional;
|
||||
|
||||
import jakarta.servlet.Filter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpFilter;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -44,7 +43,7 @@ class FilterTests {
|
||||
|
||||
Filter filter = new HttpFilter() {
|
||||
@Override
|
||||
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
|
||||
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException {
|
||||
res.getWriter().write("It works!");
|
||||
}
|
||||
};
|
||||
|
||||
+1
@@ -43,6 +43,7 @@ class HttpServerTests {
|
||||
|
||||
@BeforeEach
|
||||
void start() throws Exception {
|
||||
|
||||
HttpHandler httpHandler = RouterFunctions.toHttpHandler(
|
||||
route(GET("/test"), request -> ServerResponse.ok().bodyValue("It works!")));
|
||||
|
||||
|
||||
+3
-6
@@ -37,16 +37,13 @@ class RouterFunctionTests {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
|
||||
RouterFunction<?> route = route(GET("/test"), request ->
|
||||
ServerResponse.ok().body("It works!"));
|
||||
|
||||
void setUp() {
|
||||
RouterFunction<?> route = route(GET("/test"), request -> ServerResponse.ok().body("It works!"));
|
||||
this.testClient = RestTestClient.bindToRouterFunction(route).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() throws Exception {
|
||||
void test() {
|
||||
this.testClient.get().uri("/test")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
|
||||
Reference in New Issue
Block a user