Building an Application with Spring Boot :: Learn how to build an application with minimal configuration. https://spring.io/guides/gs/spring-boot/

HelloControllerTest.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package hello;
  2. import static org.hamcrest.Matchers.is;
  3. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  4. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.boot.test.SpringApplicationConfiguration;
  9. import org.springframework.http.MediaType;
  10. import org.springframework.mock.web.MockServletContext;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. import org.springframework.test.context.web.WebAppConfiguration;
  13. import org.springframework.test.web.servlet.MockMvc;
  14. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  15. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @SpringApplicationConfiguration(classes = MockServletContext.class)
  18. @WebAppConfiguration
  19. public class HelloControllerTest {
  20. private MockMvc mvc;
  21. @Before
  22. public void setUp() throws Exception {
  23. mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
  24. }
  25. @Test
  26. public void getHello() throws Exception {
  27. mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
  28. .andExpect(status().isOk())
  29. .andExpect(content().string(is("Greetings from Spring Boot!")));
  30. }
  31. }