Browse Source

Polishing in RestTestClient tests

See gh-34428
pull/35262/head
rstoyanchev 6 months ago
parent
commit
f57828708a
  1. 12
      spring-test/src/test/java/org/springframework/test/web/servlet/client/RestTestClientTests.java
  2. 2
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ApiVersionTests.java
  3. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ErrorTests.java
  4. 4
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/HeaderAndCookieTests.java
  5. 8
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/JsonContentTests.java
  6. 4
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/ResponseEntityTests.java
  7. 2
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/SoftAssertionTests.java
  8. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/XmlContentTests.java
  9. 4
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/ApplicationContextTests.java
  10. 3
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/FilterTests.java
  11. 1
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/HttpServerTests.java
  12. 9
      spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/RouterFunctionTests.java

12
spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/RestTestClientTests.java → spring-test/src/test/java/org/springframework/test/web/servlet/client/RestTestClientTests.java

@ -14,7 +14,7 @@ @@ -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; @@ -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 { @@ -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 { @@ -127,6 +128,7 @@ class RestTestClientTests {
}
@Nested
class Mutation {
@ -149,6 +151,7 @@ class RestTestClientTests { @@ -149,6 +151,7 @@ class RestTestClientTests {
}
}
@Nested
class Uris {
@ -193,6 +196,7 @@ class RestTestClientTests { @@ -193,6 +196,7 @@ class RestTestClientTests {
}
}
@Nested
class Cookies {
@Test
@ -214,6 +218,7 @@ class RestTestClientTests { @@ -214,6 +218,7 @@ class RestTestClientTests {
}
}
@Nested
class Headers {
@Test
@ -271,6 +276,7 @@ class RestTestClientTests { @@ -271,6 +276,7 @@ class RestTestClientTests {
}
}
@Nested
class Expectations {
@Test
@ -281,6 +287,7 @@ class RestTestClientTests { @@ -281,6 +287,7 @@ class RestTestClientTests {
}
}
@Nested
class ReturnResults {
@Test
@ -312,6 +319,7 @@ class RestTestClientTests { @@ -312,6 +319,7 @@ class RestTestClientTests {
}
}
@RestController
static class TestController {

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

@ -94,7 +94,7 @@ public class ApiVersionTests { @@ -94,7 +94,7 @@ public class ApiVersionTests {
@RestController
static class TestController {
private static class TestController {
private static final String HEADER = "X-API-Version";

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

@ -47,8 +47,9 @@ class ErrorTests { @@ -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() {

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

@ -36,6 +36,7 @@ class HeaderAndCookieTests { @@ -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 { @@ -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) {

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

@ -129,7 +129,7 @@ class JsonContentTests { @@ -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 { @@ -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;

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

@ -43,10 +43,12 @@ import static org.hamcrest.Matchers.startsWith; @@ -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 { @@ -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) {

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

@ -61,7 +61,7 @@ class SoftAssertionTests { @@ -61,7 +61,7 @@ class SoftAssertionTests {
@RestController
static class TestController {
private static class TestController {
@GetMapping("/test")
String handle() {

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

@ -172,9 +172,10 @@ class XmlContentTests { @@ -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
spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/ApplicationContextTests.java

@ -37,17 +37,21 @@ import org.springframework.web.context.WebApplicationContext; @@ -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")

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

@ -21,7 +21,6 @@ import java.util.Optional; @@ -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 { @@ -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
spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/HttpServerTests.java

@ -43,6 +43,7 @@ class HttpServerTests { @@ -43,6 +43,7 @@ class HttpServerTests {
@BeforeEach
void start() throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(
route(GET("/test"), request -> ServerResponse.ok().bodyValue("It works!")));

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

@ -37,16 +37,13 @@ class RouterFunctionTests { @@ -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()

Loading…
Cancel
Save