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; 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.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; 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 @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class EmployeeControllerContentNegotiationIntegrationTest { public class EmployeeControllerContentNegotiationIntegrationTest {
@ -23,7 +23,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonSuffixCalled_thenJsonResponseObtained() throws Exception { public void whenEndpointUsingJsonSuffixCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1.json")) this.mockMvc.perform(get("/employee/1.json"))
.andExpect(status().isOk()) .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 @Test
@ -37,7 +37,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonParameterCalled_thenJsonResponseObtained() throws Exception { public void whenEndpointUsingJsonParameterCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1?mediaType=json")) this.mockMvc.perform(get("/employee/1?mediaType=json"))
.andExpect(status().isOk()) .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 @Test
@ -51,7 +51,7 @@ public class EmployeeControllerContentNegotiationIntegrationTest {
public void whenEndpointUsingJsonAcceptHeaderCalled_thenJsonResponseObtained() throws Exception { public void whenEndpointUsingJsonAcceptHeaderCalled_thenJsonResponseObtained() throws Exception {
this.mockMvc.perform(get("/employee/1").header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)) this.mockMvc.perform(get("/employee/1").header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk()) .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 @Test

View File

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

View File

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