|
| 1 | +package com.baeldung.web.controller; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.test.context.ContextConfiguration; |
| 8 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 9 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 10 | +import org.springframework.test.web.servlet.MockMvc; |
| 11 | +import org.springframework.test.web.servlet.ResultMatcher; |
| 12 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 13 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 14 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 15 | +import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; |
| 16 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 17 | +import org.springframework.web.context.WebApplicationContext; |
| 18 | + |
| 19 | +import com.baeldung.spring.web.config.WebConfig; |
| 20 | + |
| 21 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 22 | +@WebAppConfiguration |
| 23 | +@ContextConfiguration(classes = WebConfig.class) |
| 24 | +public class RequestMapingShortcutsUnitTest { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private WebApplicationContext ctx; |
| 28 | + |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup () { |
| 33 | + DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx); |
| 34 | + this.mockMvc = builder.build(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception { |
| 39 | + |
| 40 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get"); |
| 41 | + |
| 42 | + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response"); |
| 43 | + |
| 44 | + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception { |
| 50 | + |
| 51 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post"); |
| 52 | + |
| 53 | + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response"); |
| 54 | + |
| 55 | + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception { |
| 61 | + |
| 62 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put"); |
| 63 | + |
| 64 | + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response"); |
| 65 | + |
| 66 | + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception { |
| 72 | + |
| 73 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete"); |
| 74 | + |
| 75 | + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response"); |
| 76 | + |
| 77 | + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception { |
| 83 | + |
| 84 | + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch"); |
| 85 | + |
| 86 | + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response"); |
| 87 | + |
| 88 | + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments