44import static org .hamcrest .Matchers .hasSize ;
55import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
66
7+ import java .nio .charset .Charset ;
8+
9+ import org .baeldung .config .H2JpaConfig ;
710import org .baeldung .domain .Modes ;
811import org .junit .Before ;
912import org .junit .Test ;
1922import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
2023import org .springframework .web .context .WebApplicationContext ;
2124
22- import java .nio .charset .Charset ;
23-
2425@ RunWith (SpringJUnit4ClassRunner .class )
25- @ SpringBootTest (classes = Application .class )
26+ @ SpringBootTest (classes = { Application .class , H2JpaConfig . class } )
2627@ WebAppConfiguration
2728public class SpringBootApplicationIntegrationTest {
2829 @ Autowired
@@ -31,36 +32,56 @@ public class SpringBootApplicationIntegrationTest {
3132
3233 @ Before
3334 public void setupMockMvc () {
34- mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext ).build ();
35+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
36+ .build ();
3537 }
3638
3739 @ Test
3840 public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect () throws Exception {
39- MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
41+ final MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
4042
41- mockMvc .perform (MockMvcRequestBuilders .get ("/entity/all" )).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType (contentType )).andExpect (jsonPath ("$" , hasSize (4 )));
43+ mockMvc .perform (MockMvcRequestBuilders .get ("/entity/all" ))
44+ .andExpect (MockMvcResultMatchers .status ()
45+ .isOk ())
46+ .andExpect (MockMvcResultMatchers .content ()
47+ .contentType (contentType ))
48+ .andExpect (jsonPath ("$" , hasSize (4 )));
4249 }
4350
4451 @ Test
4552 public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect () throws Exception {
46- MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
53+ final MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
4754
48- mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbydate/{date}" , "2011-12-03T10:15:30" )).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType (contentType ))
49- .andExpect (jsonPath ("$.id" , equalTo (1 )));
55+ mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbydate/{date}" , "2011-12-03T10:15:30" ))
56+ .andExpect (MockMvcResultMatchers .status ()
57+ .isOk ())
58+ .andExpect (MockMvcResultMatchers .content ()
59+ .contentType (contentType ))
60+ .andExpect (jsonPath ("$.id" , equalTo (1 )));
5061 }
5162
5263 @ Test
5364 public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect () throws Exception {
54- MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
65+ final MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
5566
56- mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbymode/{mode}" , Modes .ALPHA .name ())).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType (contentType )).andExpect (jsonPath ("$.id" , equalTo (1 )));
67+ mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbymode/{mode}" , Modes .ALPHA .name ()))
68+ .andExpect (MockMvcResultMatchers .status ()
69+ .isOk ())
70+ .andExpect (MockMvcResultMatchers .content ()
71+ .contentType (contentType ))
72+ .andExpect (jsonPath ("$.id" , equalTo (1 )));
5773 }
5874
5975 @ Test
6076 public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect () throws Exception {
61- MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
77+ final MediaType contentType = new MediaType (MediaType .APPLICATION_JSON .getType (), MediaType .APPLICATION_JSON .getSubtype (), Charset .forName ("utf8" ));
6278
63- mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbyversion" ).header ("Version" , "1.0.0" )).andExpect (MockMvcResultMatchers .status ().isOk ()).andExpect (MockMvcResultMatchers .content ().contentType (contentType ))
64- .andExpect (jsonPath ("$.id" , equalTo (1 )));
79+ mockMvc .perform (MockMvcRequestBuilders .get ("/entity/findbyversion" )
80+ .header ("Version" , "1.0.0" ))
81+ .andExpect (MockMvcResultMatchers .status ()
82+ .isOk ())
83+ .andExpect (MockMvcResultMatchers .content ()
84+ .contentType (contentType ))
85+ .andExpect (jsonPath ("$.id" , equalTo (1 )));
6586 }
6687}
0 commit comments