DemoControllerTest.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.yihu.jw.controller; //目录要和web的目录一致
  2. import org.junit.Before;
  3. import org.junit.FixMethodOrder;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.MethodSorters;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.SpringBootConfiguration;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13. import org.springframework.test.web.servlet.MockMvc;
  14. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  15. import org.springframework.web.context.WebApplicationContext;
  16. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  17. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  18. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  19. /**
  20. * @author chenweida
  21. * 目录要和web的目录一致
  22. */
  23. @RunWith(SpringRunner.class)
  24. @SpringBootTest
  25. @ComponentScan(basePackages={"com"})
  26. //@FixMethodOrder(MethodSorters.DEFAULT) //默认顺序由方法名hashcode值来决定,如果hash值大小一致,则按名字的字典顺序确定 由于hashcode的生成和操作系统相关(以native修饰),所以对于不同操作系统,可能会出现不一样的执行顺序,在某一操作系统上,多次执行的顺序不变
  27. @FixMethodOrder(MethodSorters.NAME_ASCENDING) //按照方法名字排序升序
  28. public class DemoControllerTest {
  29. @Autowired
  30. protected WebApplicationContext wac;
  31. protected MockMvc mockMvc;
  32. /**
  33. * 初始化模拟mvc环境
  34. */
  35. @Before
  36. public void setup() {
  37. mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  38. }
  39. @Test
  40. public void testAll() throws Exception {
  41. whenQuerySuccess1();
  42. whenQuerySuccess2();
  43. }
  44. /**
  45. * 测试查询
  46. * @throws Exception
  47. */
  48. //@Ignore指定忽略此测试方法
  49. public void whenQuerySuccess2() throws Exception {
  50. String result = mockMvc.perform(
  51. get("/demo")
  52. // .param("size", "15")
  53. // .param("page", "3")
  54. // .param("sort", "age,desc")
  55. .contentType(MediaType.APPLICATION_JSON_UTF8))
  56. .andExpect(status().isOk())
  57. .andReturn().getResponse().getContentAsString();
  58. System.out.println(result);
  59. System.out.println("2222222222222222222222222222");
  60. }
  61. /**
  62. * 测试查询
  63. * @throws Exception
  64. */
  65. //@Test
  66. //@Ignore指定忽略此测试方法
  67. public void whenQuerySuccess1() throws Exception {
  68. String result = mockMvc.perform(
  69. get("/demo")
  70. // .param("size", "15")
  71. // .param("page", "3")
  72. // .param("sort", "age,desc")
  73. .contentType(MediaType.APPLICATION_JSON_UTF8))
  74. .andExpect(status().isOk())
  75. .andReturn().getResponse().getContentAsString();
  76. System.out.println(result);
  77. System.out.println("111111111111111111111111111");
  78. }
  79. }