|
| 1 | + |
| 2 | +package org.baeldung.web.test; |
| 3 | + |
| 4 | +import static org.hamcrest.Matchers.hasSize; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 9 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 12 | + |
| 13 | +import org.baeldung.config.WebConfig; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.test.context.ContextConfiguration; |
| 19 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 20 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 21 | +import org.springframework.test.web.servlet.MockMvc; |
| 22 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 23 | +import org.springframework.web.context.WebApplicationContext; |
| 24 | + |
| 25 | + |
| 26 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 27 | +@ContextConfiguration(classes = WebConfig.class) |
| 28 | +@WebAppConfiguration |
| 29 | +public class BazzNewMappingsExampleControllerTest { |
| 30 | + |
| 31 | + private MockMvc mockMvc; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private WebApplicationContext webApplicationContext; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() { |
| 38 | + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenGettingAllBazz_thenSuccess() throws Exception{ |
| 43 | + mockMvc.perform(get("/bazz")) |
| 44 | + .andExpect(status().isOk()) |
| 45 | + .andExpect(jsonPath("$", hasSize(4))) |
| 46 | + .andExpect(jsonPath("$[1].id", is("2"))) |
| 47 | + .andExpect(jsonPath("$[1].name", is("Bazz2"))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenGettingABazz_thenSuccess() throws Exception{ |
| 52 | + mockMvc.perform(get("/bazz/1")) |
| 53 | + .andExpect(status().isOk()) |
| 54 | + .andExpect(jsonPath("$.id", is("1"))) |
| 55 | + .andExpect(jsonPath("$.name", is("Bazz1"))); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenAddingABazz_thenSuccess() throws Exception{ |
| 60 | + mockMvc.perform(post("/bazz").param("name", "Bazz5")) |
| 61 | + .andExpect(status().isOk()) |
| 62 | + .andExpect(jsonPath("$.id", is("5"))) |
| 63 | + .andExpect(jsonPath("$.name", is("Bazz5"))); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenUpdatingABazz_thenSuccess() throws Exception{ |
| 68 | + mockMvc.perform(put("/bazz/5").param("name", "Bazz6")) |
| 69 | + .andExpect(status().isOk()) |
| 70 | + .andExpect(jsonPath("$.id", is("5"))) |
| 71 | + .andExpect(jsonPath("$.name", is("Bazz6"))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void whenDeletingABazz_thenSuccess() throws Exception{ |
| 76 | + mockMvc.perform(delete("/bazz/5")) |
| 77 | + .andExpect(status().isOk()) |
| 78 | + .andExpect(jsonPath("$.id", is("5"))); |
| 79 | + } |
| 80 | +} |
0 commit comments