Mock接口controller单元测试记录

Mock接口controller单元测试记录:

一:创建基础BaseJunit

/**
 * 基础测试类
 *
 * @author xx
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = xxxx.class)
@WebAppConfiguration
@Transactional //打开的话测试之后数据可自动回滚
public class BaseJunit {

    @Autowired
    WebApplicationContext webApplicationContext;

    protected MockMvc mvc;


    protected String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjE2NzM5ODQ1fQ.qu0dsJ336XLm43p_QsuyZwbuAo3pYC1om3ZT7WeezfY";

    @Before
    public void setupMockMvc(){
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Before
    public void initDatabase(){
    }

    @BeforeEach
    void setUp() throws Exception {
        if(!StringUtils.isEmpty(token)){
            return;
        }

        String url = "/users/login";
        String content = "{\"userName\": \"admin\", \"passWord\": \"\"}";

        RequestBuilder request = post(url)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(content);
        MvcResult result = mvc.perform(request)
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
        assertEquals(ResCode.SUCCESS.getCode(),jsonObject.getString("status_code"));
        token = jsonObject.getJSONObject("payload").getString("token");
        TestUtil.tokens=token;
    }

    @Test
    public void Release() throws Exception {

    }

二:创建带有请求头的Utils

public class TestUtil {

    public static String tokens="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjE2NzM5ODQ1f";

    /**
     * post请求
     * @param url
     * @param content body体内容
     * @param mvc
     * @param token token
     * @return
     * @throws Exception
     */
    public static JSONObject postHttp(String url, String content, MockMvc mvc,String token) throws Exception {
        RequestBuilder request = post(url)
                .header("Authorization","token "+token)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(content);
        MvcResult result = mvc.perform(request)
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
        return jsonObject;
    }

    /**
     * get请求
     * @param url
     * @param mvc
     * @param token token
     * @return
     * @throws Exception
     */
    public static JSONObject getHttp(String url, MockMvc mvc,String token) throws Exception {
        RequestBuilder request = get(url)
                .header("Authorization","token "+token);
        MvcResult result = mvc.perform(request)
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
        return jsonObject;
    }

    /**
     * put请求
     * @param url
     * @param params 请求参数
     * @param content body体内容
     * @param mvc
     * @param token
     * @return
     * @throws Exception
     */
    public static JSONObject putHttp(String url, MultiValueMap<String, String> params, String content, MockMvc mvc,String token) throws Exception {
        RequestBuilder request = put(url)
                .header("Authorization","token "+token)
                //.param("ownerId","1")
                .params(params)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(content);
        MvcResult result = mvc.perform(request)
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
        return jsonObject;
    }

    /**
     * delete
     * @param url
     * @param content body体内容
     * @param mvc
     * @param token token
     * @return
     * @throws Exception
     */
    public static JSONObject deleteHttp(String url, String content, MockMvc mvc,String token) throws Exception {
        RequestBuilder request = delete(url)
                .header("Authorization","token "+token)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .content(content);
        MvcResult result = mvc.perform(request)
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
        return jsonObject;
    }
}

三:创建controller单元测试类

public class BaseLineControllerTest extends BaseJunit { 

private String prefix="/baseline";
 
    /** 
    * Method: list(@RequestParam(required = false) String projectId, @RequestParam(required = false) String domainCode, @RequestParam(required = false) String serviceName, @RequestParam(required = false, defaultValue = "10") Long pageSize, @RequestParam(required = false, defaultValue = "1") Long pageNum) 
    */ 
    @Test
    public void List() throws Exception { 
        String url = String.format("%s%s",prefix,"?domainCode=&serviceName=&projectId=&pageNum=1&pageSize=10");
        //String content = "{\"id\": \"1\"}";
        JSONObject jsonObject = TestUtil.getHttp(url,mvc,token);
        assertEquals(ResCode.SUCCESS.getCode(),jsonObject.getString("status_code"));
        System.out.println("list");
    } 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值