UserControllerTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //package com.yihu.jw.controller;
  2. //
  3. //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
  4. //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
  5. //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  6. //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  7. //import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
  8. //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  9. //import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  10. //
  11. //import java.time.LocalDateTime;
  12. //import java.time.ZoneId;
  13. //import java.util.Date;
  14. //
  15. //import com.yihu.jw.common.BaseTest;
  16. //import org.junit.Before;
  17. //import org.junit.Test;
  18. //import org.junit.runner.RunWith;
  19. //import org.springframework.beans.factory.annotation.Autowired;
  20. //import org.springframework.boot.test.context.SpringBootTest;
  21. //import org.springframework.http.MediaType;
  22. //import org.springframework.mock.web.MockMultipartFile;
  23. //import org.springframework.test.context.junit4.SpringRunner;
  24. //import org.springframework.test.web.servlet.MockMvc;
  25. //import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  26. //import org.springframework.web.context.WebApplicationContext;
  27. //
  28. ///**
  29. // * @author chenweida
  30. // *
  31. // */
  32. //@RunWith(SpringRunner.class)
  33. //@SpringBootTest
  34. //public class UserControllerTest extends BaseTest {
  35. //
  36. // @Autowired
  37. // protected WebApplicationContext wac;
  38. //
  39. // protected MockMvc mockMvc;
  40. //
  41. // /**
  42. // * 初始化模拟mvc环境
  43. // */
  44. // @Before
  45. // public void setup() {
  46. // mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  47. // }
  48. //
  49. // /**
  50. // * 文件上传
  51. // * @throws Exception
  52. // */
  53. // @Test
  54. // public void whenUploadSuccess() throws Exception {
  55. // String result = mockMvc.perform(fileUpload("/file") //文件上传路径
  56. // .file(new MockMultipartFile("file", "test.txt", "multipart/form-data", "hello upload".getBytes("UTF-8"))))
  57. // .andExpect(status().isOk()) //期望值OK 也就是200
  58. // .andReturn().getResponse().getContentAsString();//得到返回的值
  59. // System.out.println(result);
  60. // }
  61. //
  62. // /**
  63. // * 测试查询
  64. // * @throws Exception
  65. // */
  66. // @Test
  67. // public void whenQuerySuccess() throws Exception {
  68. // String result = mockMvc.perform(
  69. // get("/user").param("username", "jojo").param("age", "18").param("ageTo", "60").param("xxx", "yyy")
  70. // // .param("size", "15")
  71. // // .param("page", "3")
  72. // // .param("sort", "age,desc")
  73. // .contentType(MediaType.APPLICATION_JSON_UTF8))
  74. // .andExpect(status().isOk()).andExpect(jsonPath("$.length()").value(3))
  75. // .andReturn().getResponse().getContentAsString();
  76. //
  77. // System.out.println(result);
  78. // }
  79. //
  80. // @Test
  81. // public void whenGetInfoSuccess() throws Exception {
  82. // String result = mockMvc.perform(get("/user/1")
  83. // .contentType(MediaType.APPLICATION_JSON_UTF8))
  84. // .andExpect(status().isOk())
  85. // .andExpect(jsonPath("$.username").value("tom"))
  86. // .andReturn().getResponse().getContentAsString();
  87. //
  88. // System.out.println(result);
  89. // }
  90. //
  91. // @Test
  92. // public void whenGetInfoFail() throws Exception {
  93. // mockMvc.perform(get("/user/a")
  94. // .contentType(MediaType.APPLICATION_JSON_UTF8))
  95. // .andExpect(status().is4xxClientError());
  96. // }
  97. //
  98. // @Test
  99. // public void whenCreateSuccess() throws Exception {
  100. //
  101. // Date date = new Date();
  102. // System.out.println(date.getTime());
  103. // String content = "{\"username\":\"tom\",\"password\":null,\"birthday\":"+date.getTime()+"}";
  104. // String reuslt = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8)
  105. // .content(content))
  106. // .andExpect(status().isOk())
  107. // .andExpect(jsonPath("$.id").value("1"))
  108. // .andReturn().getResponse().getContentAsString();
  109. //
  110. // System.out.println(reuslt);
  111. // }
  112. //
  113. // @Test
  114. // public void whenCreateFail() throws Exception {
  115. //
  116. // Date date = new Date();
  117. // System.out.println(date.getTime());
  118. // String content = "{\"username\":\"tom\",\"password\":null,\"birthday\":"+date.getTime()+"}";
  119. // String reuslt = mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON_UTF8)
  120. // .content(content))
  121. //// .andExpect(status().isOk())
  122. //// .andExpect(jsonPath("$.id").value("1"))
  123. // .andReturn().getResponse().getContentAsString();
  124. //
  125. // System.out.println(reuslt);
  126. // }
  127. //
  128. // @Test
  129. // public void whenUpdateSuccess() throws Exception {
  130. //
  131. // Date date = new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
  132. // System.out.println(date.getTime());
  133. // String content = "{\"id\":\"1\", \"username\":\"tom\",\"password\":null,\"birthday\":"+date.getTime()+"}";
  134. // String reuslt = mockMvc.perform(put("/user/1").contentType(MediaType.APPLICATION_JSON_UTF8)
  135. // .content(content))
  136. // .andExpect(status().isOk())
  137. // .andExpect(jsonPath("$.id").value("1"))
  138. // .andReturn().getResponse().getContentAsString();
  139. //
  140. // System.out.println(reuslt);
  141. // }
  142. //
  143. // @Test
  144. // public void whenDeleteSuccess() throws Exception {
  145. // mockMvc.perform(delete("/user/1")
  146. // .contentType(MediaType.APPLICATION_JSON_UTF8))
  147. // .andExpect(status().isOk());
  148. // }
  149. //
  150. //}