1+ package org .baeldung .web .controller ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import org .baeldung .config .WebConfig ;
5+ import org .baeldung .web .dto .HeavyResource ;
6+ import org .baeldung .web .dto .HeavyResourceAddressPartialUpdate ;
7+ import org .junit .Before ;
8+ import org .junit .Test ;
9+ import org .junit .runner .RunWith ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .http .MediaType ;
12+ import org .springframework .test .context .ContextConfiguration ;
13+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
14+ import org .springframework .test .context .web .WebAppConfiguration ;
15+ import org .springframework .test .web .servlet .MockMvc ;
16+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
17+ import org .springframework .web .context .WebApplicationContext ;
18+
19+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .patch ;
20+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
21+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
22+
23+
24+ @ RunWith (SpringJUnit4ClassRunner .class )
25+ @ ContextConfiguration (classes = WebConfig .class )
26+ @ WebAppConfiguration
27+ public class HeavyResourceControllerTest {
28+
29+ private MockMvc mockMvc ;
30+
31+ @ Autowired
32+ private WebApplicationContext webApplicationContext ;
33+
34+ private final ObjectMapper objectMapper = new ObjectMapper ();
35+
36+ @ Before
37+ public void setUp () {
38+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext ).build ();
39+ }
40+
41+ @ Test
42+ public void givenHeavyResource_whenSendPutRequest_thenCreateResource () throws Exception {
43+ mockMvc .perform (put ("/heavy" )
44+ .contentType (MediaType .APPLICATION_JSON_VALUE )
45+ .content (objectMapper .writeValueAsString (new HeavyResource (1 , "Tom" , "Jackson" , 12 , "heaven street" )))
46+ ).andExpect (status ().isOk ());
47+ }
48+
49+ @ Test
50+ public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially () throws Exception {
51+ mockMvc .perform (patch ("/heavy" )
52+ .contentType (MediaType .APPLICATION_JSON_VALUE )
53+ .content (objectMapper .writeValueAsString (new HeavyResourceAddressPartialUpdate (1 , "5th avenue" )))
54+ ).andExpect (status ().isOk ());
55+ }
56+
57+ }
0 commit comments