Refactor WebTestClient response body expectations
Reduce the number of required steps and re-introduce generics support for simple Class<T> cases.
This commit is contained in:
@@ -38,7 +38,7 @@ public class DefaultControllerSpecTests {
|
||||
.get().uri("/")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("Success");
|
||||
.expectBody(String.class).isEqualTo("Success");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -49,7 +49,7 @@ public class DefaultControllerSpecTests {
|
||||
.get().uri("/exception")
|
||||
.exchange()
|
||||
.expectStatus().isBadRequest()
|
||||
.expectBody(String.class).value().isEqualTo("Handled exception");
|
||||
.expectBody(String.class).isEqualTo("Handled exception");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -40,9 +40,10 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static java.time.Duration.*;
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.core.ResolvableType.forClassWithGenerics;
|
||||
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
|
||||
|
||||
/**
|
||||
@@ -62,7 +63,7 @@ public class ResponseEntityTests {
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.expectBody(Person.class).value().isEqualTo(new Person("John"));
|
||||
.expectBody(Person.class).isEqualTo(new Person("John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,7 +76,7 @@ public class ResponseEntityTests {
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
|
||||
.expectBody(Person.class).list().isEqualTo(expected);
|
||||
.expectBodyList(Person.class).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,8 +90,7 @@ public class ResponseEntityTests {
|
||||
this.client.get().uri("/persons?map=true")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody()
|
||||
.map(String.class, Person.class).isEqualTo(map);
|
||||
.expectBody(forClassWithGenerics(Map.class, String.class, Person.class)).isEqualTo(map);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,8 +102,7 @@ public class ResponseEntityTests {
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectHeader().contentType(TEXT_EVENT_STREAM)
|
||||
.expectBody(Person.class)
|
||||
.returnResult();
|
||||
.returnResult(Person.class);
|
||||
|
||||
StepVerifier.create(result.getResponseBody())
|
||||
.expectNext(new Person("N0"), new Person("N1"), new Person("N2"))
|
||||
@@ -126,6 +125,7 @@ public class ResponseEntityTests {
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/persons")
|
||||
@SuppressWarnings("unused")
|
||||
static class PersonController {
|
||||
|
||||
@GetMapping("/{name}")
|
||||
|
||||
@@ -63,7 +63,7 @@ public class ApplicationContextTests {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Pablo!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,7 +72,7 @@ public class ApplicationContextTests {
|
||||
.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Giovanni!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,7 +83,7 @@ public class ApplicationContextTests {
|
||||
.get().uri("/attributes")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("foo+bar");
|
||||
.expectBody(String.class).isEqualTo("foo+bar");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ControllerTests {
|
||||
this.client.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Pablo!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -57,7 +57,7 @@ public class ControllerTests {
|
||||
.get().uri("/principal")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
|
||||
.expectBody(String.class).isEqualTo("Hello Mr. Giovanni!");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -68,7 +68,7 @@ public class ControllerTests {
|
||||
.get().uri("/attributes")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("foo+bar");
|
||||
.expectBody(String.class).isEqualTo("foo+bar");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ public class HttpServerTests {
|
||||
this.client.get().uri("/test")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("It works!");
|
||||
.expectBody(String.class).isEqualTo("It works!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class RouterFunctionTests {
|
||||
this.testClient.get().uri("/test")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).value().isEqualTo("It works!");
|
||||
.expectBody(String.class).isEqualTo("It works!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user