Spring Boot(Cloud ) 测试类Test示例 MockMvc

Spring Boot(Cloud ) 测试类Test示例 MockMvc

get请求工具类:


import com.credithc.common.util.FastJsonUtil;
import org.junit.Assert;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;

import java.io.UnsupportedEncodingException;

/**
 * @Author: 
 * @Description:
 * @Date: Created in 18:31 2018/8/20
 */
public class CommonTestUtil {

    public static String token = "eyJhbG";

    /**
     * get类型的方法,测试访问
     * @param mockMvc
     * @param flag
     * @param url
     * @throws Exception
     */
    public static void commonGet(MockMvc mockMvc, String flag, String url){
        long start = System.currentTimeMillis();
        MvcResult mvcResult = null;
        try {
            mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(url).header("access-token",token))
                    .andDo(MockMvcResultHandlers.log())
                    .andReturn();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int status = mvcResult.getResponse().getStatus();
        mvcResult.getResponse().setCharacterEncoding("UTF-8");
        String contentAsString = null;
        try {
            contentAsString = mvcResult.getResponse().getContentAsString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Assert.assertEquals("请求错误",200,status);

        System.out.println();
        System.out.println("--------"+flag +":"+ FastJsonUtil.obj2json(contentAsString));
        long end = System.currentTimeMillis();
        System.out.println("花费时长:"+(end-start));
    }
}

测试类:


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.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 com.credithc.inner.CommonTestUtil.commonGet;

/**
 * @Author: 
 * @Description:
 * @Date: Created in 18:03 2018/8/20
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = 项目启动类名.class)
public class CrmPresonaDkRestTest {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

/**
* get请求方式
*/
    @Test
    public void getAll() throws Exception {
        commonGet(mockMvc,"getAll","/department/getAll");
    }

/**
* post请求方式
*/
    @Test
    public void update() throws Exception {
        Resource resource = new Resource();
        resource.setName("aa4");
        resource.setId(11258);
        resource.setNumber("1234");
        resource.setUrl("aa4");
        resource.setPid(1);
        resource.setRequestType("get4");
        resource.setType(1);

        //1
//        ObjectMapper mapper = new ObjectMapper();
//        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
//        java.lang.String requestJson = ow.writeValueAsString(resource);
        //2
        String requestJson = JSONObject.toJSONString(resource);

        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/resource/update")
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestJson).header("access-token", CommonTestUtil.token)
        )
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().is(200))
                .andReturn();
        result.getResponse().setCharacterEncoding("UTF-8");
        System.out.println(result.getResponse().getContentAsString());
    }

}

你可能感兴趣的:(springCloud,spring)