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 org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import org.springframework.test.web.servlet.MockMvc;
  10. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  11. import static org.hamcrest.Matchers.equalTo;
  12. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  13. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  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 the great unknown!")));
  25. }
  26. }