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+
4+
5+
6+ import java .time .LocalDateTime ;
7+
8+ import org .springframework .boot .SpringApplication ;
9+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
10+ import org .springframework .boot .builder .SpringApplicationBuilder ;
11+ import org .springframework .boot .web .support .SpringBootServletInitializer ;
12+ import org .springframework .ui .Model ;
13+ import org .springframework .web .bind .annotation .RequestMapping ;
14+ import org .springframework .web .bind .annotation .RestController ;
15+
16+ @ SpringBootApplication
17+ public class WarInitializerApplication extends SpringBootServletInitializer {
18+
19+ @ Override
20+ protected SpringApplicationBuilder configure (SpringApplicationBuilder builder ) {
21+ return builder .sources (WarInitializerApplication .class );
22+ }
23+
24+ public static void main (String [] args ) {
25+ SpringApplication sa = new SpringApplication (WarInitializerApplication .class );
26+ sa .setLogStartupInfo (false );
27+ sa .run (args );
28+ }
29+
30+ @ RestController
31+ public static class WarInitializerController {
32+
33+ @ RequestMapping ("/" )
34+ public String handler (Model model ) {
35+ model .addAttribute ("date" ,
36+ LocalDateTime .now ());
37+ return "WarInitializerApplication is up and running!" ;
38+ }
39+ }
40+ }
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