Merge pull request #4070 from enpy303/master

A Quick Intro to the SpringBootServletInitializer
This commit is contained in:
Loredana Crusoveanu 2018-04-27 13:58:35 +03:00 committed by GitHub
commit f514212ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 0 deletions

View File

@ -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!";
}
}
}

View File

@ -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!")));
}
}