|
| 1 | +package com.baeldung.web; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.test.context.junit4.SpringRunner; |
| 10 | +import org.springframework.test.web.servlet.MockMvc; |
| 11 | +import org.springframework.web.server.MediaTypeNotSupportedStatusException; |
| 12 | + |
| 13 | +import com.baeldung.web.controller.students.Student; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | + |
| 16 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
| 22 | + |
| 23 | +@RunWith(SpringRunner.class) |
| 24 | +@SpringBootTest |
| 25 | +@AutoConfigureMockMvc |
| 26 | +public class StudentControllerIntegrationTest { |
| 27 | + |
| 28 | + private static final String STUDENTS_PATH = "/students/"; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private MockMvc mockMvc; |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenReadAll_thenStatusIsOk() throws Exception { |
| 35 | + this.mockMvc.perform(get(STUDENTS_PATH)) |
| 36 | + .andExpect(status().isOk()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenReadOne_thenStatusIsOk() throws Exception { |
| 41 | + this.mockMvc.perform(get(STUDENTS_PATH + 1)) |
| 42 | + .andExpect(status().isOk()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenCreate_thenStatusIsCreated() throws Exception { |
| 47 | + Student student = new Student(10, "Albert", "Einstein"); |
| 48 | + this.mockMvc.perform(post(STUDENTS_PATH).content(asJsonString(student)) |
| 49 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 50 | + .andExpect(status().isCreated()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenUpdate_thenStatusIsOk() throws Exception { |
| 55 | + Student student = new Student(1, "Nikola", "Tesla"); |
| 56 | + this.mockMvc.perform(put(STUDENTS_PATH + 1) |
| 57 | + .content(asJsonString(student)) |
| 58 | + .contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 59 | + .andExpect(status().isOk()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenDelete_thenStatusIsNoContent() throws Exception { |
| 64 | + this.mockMvc.perform(delete(STUDENTS_PATH + 3)) |
| 65 | + .andExpect(status().isNoContent()); |
| 66 | + } |
| 67 | + |
| 68 | + private String asJsonString(final Object obj) { |
| 69 | + try { |
| 70 | + return new ObjectMapper().writeValueAsString(obj); |
| 71 | + } catch (Exception e) { |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments