Merge pull request #4070 from enpy303/master
A Quick Intro to the SpringBootServletInitializer
This commit is contained in:
commit
f514212ceb
|
@ -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!";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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!")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue