BAEL-21543: Fix integration tests in spring-boot

This commit is contained in:
Krzysiek 2020-01-27 20:04:21 +01:00
parent 78df7c0aed
commit e8dba2ffd6

View File

@ -2,9 +2,6 @@ package com.baeldung.beanvalidation.application;
import com.baeldung.beanvalidation.application.controllers.UserController; import com.baeldung.beanvalidation.application.controllers.UserController;
import com.baeldung.beanvalidation.application.repositories.UserRepository; import com.baeldung.beanvalidation.application.repositories.UserRepository;
import java.nio.charset.Charset;
import static org.assertj.core.api.Assertions.assertThat;
import org.hamcrest.core.Is; import org.hamcrest.core.Is;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -18,6 +15,10 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.nio.charset.Charset;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@WebMvcTest @WebMvcTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
@ -40,9 +41,9 @@ public class UserControllerIntegrationTest {
@Test @Test
public void whenGetRequestToUsers_thenCorrectResponse() throws Exception { public void whenGetRequestToUsers_thenCorrectResponse() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/users") mockMvc.perform(MockMvcRequestBuilders.get("/users")
.contentType(MediaType.APPLICATION_JSON_UTF8)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)); .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
} }
@ -52,7 +53,7 @@ public class UserControllerIntegrationTest {
String user = "{\"name\": \"bob\", \"email\" : \"bob@domain.com\"}"; String user = "{\"name\": \"bob\", \"email\" : \"bob@domain.com\"}";
mockMvc.perform(MockMvcRequestBuilders.post("/users") mockMvc.perform(MockMvcRequestBuilders.post("/users")
.content(user) .content(user)
.contentType(MediaType.APPLICATION_JSON_UTF8)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8)); .andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8));
} }
@ -62,9 +63,9 @@ public class UserControllerIntegrationTest {
String user = "{\"name\": \"\", \"email\" : \"bob@domain.com\"}"; String user = "{\"name\": \"\", \"email\" : \"bob@domain.com\"}";
mockMvc.perform(MockMvcRequestBuilders.post("/users") mockMvc.perform(MockMvcRequestBuilders.post("/users")
.content(user) .content(user)
.contentType(MediaType.APPLICATION_JSON_UTF8)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isBadRequest()) .andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("Name is mandatory"))) .andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("Name is mandatory")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)); .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
} }
} }