DemoControllerTest.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.yihu.jw.controller;
  2. import com.yihu.jw.common.BaseTest;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.SpringBootConfiguration;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.http.MediaType;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11. import org.springframework.test.web.servlet.MockMvc;
  12. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  13. import org.springframework.web.context.WebApplicationContext;
  14. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  15. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  16. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  17. /**
  18. * @author chenweida
  19. *
  20. */
  21. @RunWith(SpringRunner.class)
  22. @SpringBootTest
  23. public class DemoControllerTest extends BaseTest {
  24. @Autowired
  25. protected WebApplicationContext wac;
  26. protected MockMvc mockMvc;
  27. /**
  28. * 初始化模拟mvc环境
  29. */
  30. @Before
  31. public void setup() {
  32. mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  33. }
  34. /**
  35. * 测试查询
  36. * @throws Exception
  37. */
  38. @Test
  39. public void whenQuerySuccess() throws Exception {
  40. String result = mockMvc.perform(
  41. get("/demo")
  42. // .param("size", "15")
  43. // .param("page", "3")
  44. // .param("sort", "age,desc")
  45. .contentType(MediaType.APPLICATION_JSON_UTF8))
  46. .andExpect(status().isOk())
  47. .andReturn().getResponse().getContentAsString();
  48. System.out.println(result);
  49. }
  50. }