HelloTests.java 1.4 KB

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