BAEL-21543: Fix integration tests in spring-mvc-basics

This commit is contained in:
Krzysiek 2020-01-27 19:34:14 +01:00
parent 0648ba53ab
commit 730d63d3ae
3 changed files with 17 additions and 21 deletions

View File

@ -1,9 +1,5 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -12,6 +8,10 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
public class EmployeeControllerContentNegotiationIntegrationTest {
@ -23,7 +23,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonSuffixCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1.json"))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
}
@Test
@ -37,7 +37,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonParameterCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1?mediaType=json"))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
}
@Test
@ -51,7 +51,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonAcceptHeaderCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1").header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE));
.andExpect(header().string(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
}
@Test

View File

@ -1,19 +1,17 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class SimpleBookControllerIntegrationTest {
private MockMvc mockMvc;
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@BeforeEach
public void setup() {
@ -25,7 +23,7 @@ public class SimpleBookControllerIntegrationTest {
this.mockMvc
.perform(get("/books/42"))
.andExpect(status().isOk())
.andExpect(content().contentType(CONTENT_TYPE))
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.id").value(42));
}

View File

@ -1,19 +1,17 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class SimpleBookRestControllerIntegrationTest {
private MockMvc mockMvc;
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
@BeforeEach
public void setup() {
@ -25,7 +23,7 @@ public class SimpleBookRestControllerIntegrationTest {
this.mockMvc
.perform(get("/books-rest/42"))
.andExpect(status().isOk())
.andExpect(content().contentType(CONTENT_TYPE))
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.id").value(42));
}