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.1KB

1234567891011121314151617181920212223242526272829303132
  1. package hello;
  2. import static org.hamcrest.Matchers.equalTo;
  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.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import org.springframework.test.web.servlet.MockMvc;
  13. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest
  16. @AutoConfigureMockMvc
  17. public class HelloControllerTest {
  18. @Autowired
  19. private MockMvc mvc;
  20. @Test
  21. public void getHello() throws Exception {
  22. mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
  23. .andExpect(status().isOk())
  24. .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
  25. }
  26. }