2021与蓝度共同重构项目,服务端
liuhaonan
2022-01-12 a4e8fdfb30294b4812ef972606e7ae7ff5d2b5f8
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
package com.sandu.ximon.admin.base;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
 
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
 
/**
 * @author chenjiantian
 * @date 2022/1/12 11:20
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;
 
    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }
 
    public MockMvc getMvc() {
        return mockMvc;
    }
 
 
    public void checkPost(String url,Object param) throws Exception{
        MockHttpServletRequestBuilder builder = BaseMockMvcRequestBuilders.post(url);
        if(param != null){
            builder.content((new ObjectMapper()).valueToTree(param).toString());//请求的参数
        }
        ResultActions resultActions = mockMvc.perform(builder);
        JSONObject response = ResultActionsUtil.getResponse(resultActions);
        //打印全部信息
        resultActions.andDo(print());
        //对数据进行预期的判断
        Assert.assertEquals("20000", response.getString("code"));
    }
 
    public void checkGet(String url) throws Exception{
        MockHttpServletRequestBuilder builder = BaseMockMvcRequestBuilders.get(url);
        ResultActions resultActions = mockMvc.perform(builder);
        JSONObject response = ResultActionsUtil.getResponse(resultActions);
        //打印全部信息
        resultActions.andDo(print());
        //对数据进行预期的判断
        Assert.assertEquals("20000", response.getString("code"));
    }
}