forked from chenjiangtao/veryjava.spring.boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.java
More file actions
74 lines (56 loc) · 2.31 KB
/
Copy pathBaseTest.java
File metadata and controls
74 lines (56 loc) · 2.31 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
package cn.veryjava;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
public class BaseTest {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setUp() {
this.setMvc(MockMvcBuilders.webAppContextSetup(this.context).build());
}
public MockHttpServletResponse doGet(String url) throws Exception {
ResultActions ra = this.getMvc().perform(get(url));
return ra.andReturn().getResponse();
}
public MockHttpServletResponse doPost(String url, Object params) throws Exception {
ObjectMapper om = new ObjectMapper();
ResultActions ra = this.getMvc()
.perform(post(url).content(om.writeValueAsString(params)).contentType(MediaType
.APPLICATION_JSON));
return ra.andReturn().getResponse();
}
public MockHttpServletResponse doPatch(String url, Object params) throws Exception {
ObjectMapper om = new ObjectMapper();
ResultActions ra = this.getMvc()
.perform(patch(url).content(om.writeValueAsString(params)).contentType(MediaType
.APPLICATION_JSON));
return ra.andReturn().getResponse();
}
public MockHttpServletResponse doPut(String url, Object params) throws Exception {
ObjectMapper om = new ObjectMapper();
ResultActions ra = this.getMvc().perform(put(url, om.writeValueAsString(params))
.contentType(MediaType.APPLICATION_JSON));
return ra.andReturn().getResponse();
}
public MockHttpServletResponse doDelete(String url, Object params) throws Exception {
ObjectMapper om = new ObjectMapper();
ResultActions ra = this.getMvc().perform(delete(url, om.writeValueAsString(params))
.contentType(MediaType.APPLICATION_JSON));
return ra.andReturn().getResponse();
}
public MockMvc getMvc() {
return mvc;
}
public void setMvc(MockMvc mvc) {
this.mvc = mvc;
}
}