|
| 1 | +package demo.springboot.test; |
| 2 | + |
| 3 | +import javax.servlet.http.Cookie; |
| 4 | + |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.mock.web.MockHttpSession; |
| 12 | +import org.springframework.test.context.junit4.SpringRunner; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 15 | +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; |
| 16 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 17 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 18 | +import org.springframework.transaction.annotation.Transactional; |
| 19 | +import org.springframework.web.context.WebApplicationContext; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.jayway.jsonpath.JsonPath; |
| 23 | + |
| 24 | +import demo.springboot.test.domain.User; |
| 25 | + |
| 26 | +@RunWith(SpringRunner.class) |
| 27 | +@SpringBootTest |
| 28 | +public class UserControllerTest { |
| 29 | + |
| 30 | + private MockMvc mockMvc; |
| 31 | + private MockHttpSession session; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private WebApplicationContext wac; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + ObjectMapper mapper; |
| 38 | + |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setupMockMvc(){ |
| 42 | + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
| 43 | + session = new MockHttpSession(); |
| 44 | + User user =new User(); |
| 45 | + user.setUsername("Dopa"); |
| 46 | + user.setPasswd("ac3af72d9f95161a502fd326865c2f15"); |
| 47 | + session.setAttribute("user",user); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @Transactional |
| 52 | + public void test() throws Exception { |
| 53 | +// mockMvc.perform( |
| 54 | +// MockMvcRequestBuilders.get("/user/{userName}", "scott") |
| 55 | +// .contentType(MediaType.APPLICATION_JSON_UTF8)) |
| 56 | +// .andExpect(MockMvcResultMatchers.status().isOk()) |
| 57 | +// .andExpect(MockMvcResultMatchers.jsonPath("$.username").value("scott")) |
| 58 | +// .andDo(MockMvcResultHandlers.print()); |
| 59 | + |
| 60 | +// String jsonStr = "{\"username\":\"Dopa\",\"passwd\":\"ac3af72d9f95161a502fd326865c2f15\",\"status\":\"1\"}"; |
| 61 | + |
| 62 | + User user = new User(); |
| 63 | + user.setUsername("Dopa"); |
| 64 | + user.setPasswd("ac3af72d9f95161a502fd326865c2f15"); |
| 65 | + user.setStatus("1"); |
| 66 | + |
| 67 | + String userJson = mapper.writeValueAsString(user); |
| 68 | + |
| 69 | + |
| 70 | +// mockMvc.perform(MockMvcRequestBuilders.post("/user/save").content(jsonStr.getBytes())); |
| 71 | + |
| 72 | + mockMvc.perform( |
| 73 | + MockMvcRequestBuilders.post("/user/save") |
| 74 | + .contentType(MediaType.APPLICATION_JSON_UTF8) |
| 75 | + .content(userJson.getBytes())) |
| 76 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 77 | + .andDo(MockMvcResultHandlers.print()); |
| 78 | + |
| 79 | +// mockMvc.perform(MockMvcRequestBuilders.get("/hello?name={name}","mrbird")); |
| 80 | +// mockMvc.perform(MockMvcRequestBuilders.post("/user/{id}", 1)); |
| 81 | +// mockMvc.perform(MockMvcRequestBuilders.fileUpload("/fileupload").file("file", "文件内容".getBytes("utf-8"))); |
| 82 | +// mockMvc.perform(MockMvcRequestBuilders.get("/hello").param("message", "hello")); |
| 83 | +// mockMvc.perform(MockMvcRequestBuilders.get("/hobby/save").param("hobby", "sleep", "eat")); |
| 84 | + |
| 85 | +// MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); |
| 86 | +// params.add("name", "mrbird"); |
| 87 | +// params.add("hobby", "sleep"); |
| 88 | +// params.add("hobby", "eat"); |
| 89 | +// mockMvc.perform(MockMvcRequestBuilders.get("/hobby/save").params(params)); |
| 90 | +// mockMvc.perform(MockMvcRequestBuilders.get("/index").sessionAttr(name, value)); |
| 91 | +// mockMvc.perform(MockMvcRequestBuilders.get("/index").cookie(new Cookie(name, value))); |
| 92 | +// mockMvc.perform(MockMvcRequestBuilders.get("/index").contentType(MediaType.APPLICATION_JSON_UTF8)); |
| 93 | +// mockMvc.perform(MockMvcRequestBuilders.get("/user/{id}", 1).accept(MediaType.APPLICATION_JSON)); |
| 94 | +// mockMvc.perform(MockMvcRequestBuilders.get("/user/{id}", 1).header(name, values)); |
| 95 | + |
| 96 | +// mockMvc.perform(MockMvcRequestBuilders.get("/index")) |
| 97 | +// .andDo(MockMvcResultHandlers.print()); |
| 98 | + |
| 99 | + |
| 100 | + } |
| 101 | +} |
0 commit comments