diff --git a/spring-boot/src/main/java/com/baeldung/servletinitializer/WarInitializerApplication.java b/spring-boot/src/main/java/com/baeldung/servletinitializer/WarInitializerApplication.java new file mode 100644 index 0000000000..237026780c --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/servletinitializer/WarInitializerApplication.java @@ -0,0 +1,36 @@ +package com.baeldung.servletinitializer; + +import java.time.LocalDateTime; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +public class WarInitializerApplication extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { + return builder.sources(WarInitializerApplication.class); + } + + public static void main(String[] args) { + SpringApplication sa = new SpringApplication(WarInitializerApplication.class); + sa.setLogStartupInfo(false); + sa.run(args); + } + + @RestController + public static class WarInitializerController { + + @RequestMapping("/") + public String handler(Model model) { + model.addAttribute("date", LocalDateTime.now()); + return "WarInitializerApplication is up and running!"; + } + } +} diff --git a/spring-boot/src/test/java/com/baeldung/servletinitializer/WarInitializerApplicationTest.java b/spring-boot/src/test/java/com/baeldung/servletinitializer/WarInitializerApplicationTest.java new file mode 100644 index 0000000000..a3ee30ef49 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/servletinitializer/WarInitializerApplicationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.servletinitializer; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import com.baeldung.servletinitializer.WarInitializerApplication.WarInitializerController; + +@RunWith(SpringRunner.class) +@WebMvcTest(controllers = WarInitializerController.class) +public class WarInitializerApplicationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenContextRootUrlIsAccessed_thenStatusIsOk() throws Exception { + mockMvc.perform(get("/")) + .andExpect(status().is(200)); + } + + @Test + public void whenContextRootUrlIsAccesed_thenCorrectStringIsReturned() throws Exception { + mockMvc.perform(get("/")) + .andExpect(content().string(containsString("WarInitializerApplication is up and running!"))); + } + +}