|
| 1 | +package com.baeldung.validation.tests; |
| 2 | + |
| 3 | +import com.baeldung.validation.application.controllers.UserController; |
| 4 | +import com.baeldung.validation.application.repositories.UserRepository; |
| 5 | +import java.nio.charset.Charset; |
| 6 | +import static org.assertj.core.api.Assertions.assertThat; |
| 7 | +import org.hamcrest.core.Is; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.test.context.junit4.SpringRunner; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 18 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 19 | + |
| 20 | +@RunWith(SpringRunner.class) |
| 21 | +@WebMvcTest |
| 22 | +@AutoConfigureMockMvc |
| 23 | +public class UserControllerIntegrationTest { |
| 24 | + |
| 25 | + @MockBean |
| 26 | + private UserRepository userRepository; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + UserController userController; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private MockMvc mockMvc; |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenUserControllerInjected_thenNotNull() throws Exception { |
| 36 | + assertThat(userController).isNotNull(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenGetRequestToUsers_thenCorrectResponse() throws Exception { |
| 41 | + mockMvc.perform(MockMvcRequestBuilders.get("/users") |
| 42 | + .contentType(MediaType.APPLICATION_JSON_UTF8)) |
| 43 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 44 | + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void whenPostRequestToUsersAndValidUser_thenCorrectResponse() throws Exception { |
| 50 | + MediaType textPlainUtf8 = new MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8")); |
| 51 | + String user = "{\"name\": \"bob\", \"email\" : \"bob@domain.com\"}"; |
| 52 | + mockMvc.perform(MockMvcRequestBuilders.post("/users") |
| 53 | + .content(user) |
| 54 | + .contentType(MediaType.APPLICATION_JSON_UTF8)) |
| 55 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 56 | + .andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenPostRequestToUsersAndInValidUser_thenCorrectReponse() throws Exception { |
| 61 | + String user = "{\"name\": \"\", \"email\" : \"bob@domain.com\"}"; |
| 62 | + mockMvc.perform(MockMvcRequestBuilders.post("/users") |
| 63 | + .content(user) |
| 64 | + .contentType(MediaType.APPLICATION_JSON_UTF8)) |
| 65 | + .andExpect(MockMvcResultMatchers.status().isBadRequest()) |
| 66 | + .andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("Name is mandatory"))) |
| 67 | + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)); |
| 68 | + } |
| 69 | +} |
0 commit comments