Java 16 migration for Web examples.

See #606.
This commit is contained in:
Mark Paluch
2021-04-29 10:55:27 +02:00
parent af86e0219f
commit 338d9d92ca
8 changed files with 23 additions and 34 deletions

View File

@@ -46,9 +46,8 @@ import static org.assertj.core.api.Assertions.*;
* @author Divya Srivastava
* @author Jens Schauder
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class UserControllerClientTests {
class UserControllerClientTests {
@Autowired TestRestTemplate template;
@@ -71,28 +70,28 @@ public class UserControllerClientTests {
}
@Test
public void accessJsonFieldsOnSimplePayload() {
void accessJsonFieldsOnSimplePayload() {
assertDave(issueGet("/", MediaType.APPLICATION_JSON));
}
@Test
public void accessJsonFieldsOnNestedPayload() {
void accessJsonFieldsOnNestedPayload() {
assertDave(issueGet("/changed", MediaType.APPLICATION_JSON));
}
@Test
public void accessXmlElementsOnSimplePayload() {
void accessXmlElementsOnSimplePayload() {
assertDave(issueGet("/", MediaType.APPLICATION_XML));
}
@Test
public void accessXmlElementsOnNestedPayload() {
void accessXmlElementsOnNestedPayload() {
assertDave(issueGet("/changed", MediaType.APPLICATION_XML));
}
private UserPayload issueGet(String path, MediaType mediaType) {
HttpHeaders headers = new HttpHeaders();
var headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT, mediaType.toString());
return template.exchange(path, HttpMethod.GET, new HttpEntity<Void>(headers), UserPayload.class).getBody();

View File

@@ -20,15 +20,13 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
/**
* Integration tests for {@link UserController}.
@@ -36,33 +34,32 @@ import org.springframework.test.web.servlet.ResultActions;
* @author Oliver Gierke
* @author Divya Srivastava
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
public class UserControllerIntegrationTests {
class UserControllerIntegrationTests {
@Autowired MockMvc mvc;
@Test
public void handlesJsonPayloadWithExactProperties() throws Exception {
void handlesJsonPayloadWithExactProperties() throws Exception {
postAndExpect("{ \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\" }", MediaType.APPLICATION_JSON);
}
@Test
public void handlesJsonPayloadWithNestedProperties() throws Exception {
void handlesJsonPayloadWithNestedProperties() throws Exception {
postAndExpect("{ \"user\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\" } }",
MediaType.APPLICATION_JSON);
}
@Test
public void handlesXmlPayLoadWithExactProperties() throws Exception {
void handlesXmlPayLoadWithExactProperties() throws Exception {
postAndExpect("<user><firstname>Dave</firstname><lastname>Matthews</lastname></user>", MediaType.APPLICATION_XML);
}
private void postAndExpect(String payload, MediaType mediaType) throws Exception {
ResultActions actions = mvc.perform(post("/")//
var actions = mvc.perform(post("/")//
.content(payload)//
.contentType(mediaType))//
.andExpect(status().isOk());