File tree Expand file tree Collapse file tree
main/java/com/baeldung/servletinitializer
test/java/com/baeldung/servletinitializer Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .servletinitializer ;
2+
3+ import java .time .LocalDateTime ;
4+
5+ import org .springframework .boot .SpringApplication ;
6+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
7+ import org .springframework .boot .builder .SpringApplicationBuilder ;
8+ import org .springframework .boot .web .support .SpringBootServletInitializer ;
9+ import org .springframework .ui .Model ;
10+ import org .springframework .web .bind .annotation .RequestMapping ;
11+ import org .springframework .web .bind .annotation .RestController ;
12+
13+ @ SpringBootApplication
14+ public class WarInitializerApplication extends SpringBootServletInitializer {
15+
16+ @ Override
17+ protected SpringApplicationBuilder configure (SpringApplicationBuilder builder ) {
18+ return builder .sources (WarInitializerApplication .class );
19+ }
20+
21+ public static void main (String [] args ) {
22+ SpringApplication sa = new SpringApplication (WarInitializerApplication .class );
23+ sa .setLogStartupInfo (false );
24+ sa .run (args );
25+ }
26+
27+ @ RestController
28+ public static class WarInitializerController {
29+
30+ @ RequestMapping ("/" )
31+ public String handler (Model model ) {
32+ model .addAttribute ("date" , LocalDateTime .now ());
33+ return "WarInitializerApplication is up and running!" ;
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .servletinitializer ;
2+
3+ import static org .hamcrest .CoreMatchers .containsString ;
4+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
7+
8+ import org .junit .Test ;
9+ import org .junit .runner .RunWith ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
12+ import org .springframework .test .context .junit4 .SpringRunner ;
13+ import org .springframework .test .web .servlet .MockMvc ;
14+
15+ import com .baeldung .servletinitializer .WarInitializerApplication .WarInitializerController ;
16+
17+ @ RunWith (SpringRunner .class )
18+ @ WebMvcTest (controllers = WarInitializerController .class )
19+ public class WarInitializerApplicationTest {
20+
21+ @ Autowired
22+ private MockMvc mockMvc ;
23+
24+ @ Test
25+ public void whenContextRootUrlIsAccessed_thenStatusIsOk () throws Exception {
26+ mockMvc .perform (get ("/" ))
27+ .andExpect (status ().is (200 ));
28+ }
29+
30+ @ Test
31+ public void whenContextRootUrlIsAccesed_thenCorrectStringIsReturned () throws Exception {
32+ mockMvc .perform (get ("/" ))
33+ .andExpect (content ().string (containsString ("WarInitializerApplication is up and running!" )));
34+ }
35+
36+ }
You can’t perform that action at this time.
0 commit comments