UserControllerTest.java
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.dituhui.mp.user;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenQueryUserSuccess() throws Exception {
String result = mockMvc.perform(
get("/v1/user/get").param("id", "40288ac97673e23f017673e26e3c0000")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
@Test
public void whenAddUserSuccess() throws Exception {
String content = "{\"password\":\"123456789\",\"phone\":\"18600707186\",\"nickname\":\"地图旅行者\",\"account\":\"18600707186\",\"sex\":1,\"teamId\":\"40288ac97673e23f017673e26ed90001\"}";
String result = mockMvc.perform(post("/v1/user/add").contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
@Test
public void whenUpdateUserSuccess() throws Exception {
String content = "{\"password\":\"123456789\",\"phone\":\"18600707187\",\"nickname\":\"地图王者\",\"account\":\"18600707187\",\"sex\":1,\"id\":\"40288ac97673e23f017673e26e3c0000\"}";
String result = mockMvc.perform(post("/v1/user/update").contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
public void whenDeleteUserSuccess() throws Exception {
String result = mockMvc.perform(post("/v1/user/delete").param("id", "40288ac97673e23f017673e26e3c0000")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
}