使用MockMvc测试Rest请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013719012/article/details/81512929

基于SpringBoot

package share.procon.modular.project.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;


/**
 * Spring boot test class
 *
 * @author yue
 * @date 2018年08月08日 15:03
 * Copyright:2018-版权所有
 */

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class EntMajorControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private String Authorization = "";
    private JSONObject jsonObject = new JSONObject();




    @Before
    public void getAuthorization() throws IOException {
        getToken();
    }


    @Test
    @Transactional
    public void addEntItem() throws Exception {
        jsonObject.put("entMname","软件工程");
        jsonObject.put("entMcode","12345");

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/entMajor/addEntMajor")
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", Authorization)
                //Rest风格提交
                .content(jsonObject.toString().getBytes())
        );

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andDo(MockMvcResultHandlers.print());
    }

    /*@Test
    public void listEntMajor() {
    }*/


    /**
     * 获取token
     *
     * @param
     * @author yue
     * @Date 2018/8/8 16:38
     */
    private void getToken() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse httpResponse = null;
        HttpPost httpPost = new HttpPost("http://127.0.0.1:8000/authAction/shareAuth");

        //表单方式
       /* List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("userName", "admin@test"));
        nameValuePairs.add(new BasicNameValuePair("password", "MTIzNDU2"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));*/

        //json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("userName", "admin@test");
        jsonParam.put("password", "MTIzNDU2");
        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");

        httpPost.setEntity(entity);
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        byte[] b = EntityUtils.toByteArray(httpEntity);
        JSONObject jsonObject = JSONObject.parseObject(b, JSONObject.class);
        Authorization = jsonObject.getJSONObject("data").getString("token");

        System.out.println("获取到的token-----------------------:" + Authorization);

        httpClient.close();

    }
}

猜你喜欢

转载自blog.csdn.net/u013719012/article/details/81512929