springboot Mock 测试 controller层

package com.uquanba.coupon;

import com.taobao.api.request.TbkDgItemCouponGetRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

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

    private static String url="xxx";
    private static String appkey="xxx";
    private static String secret="xxx";

    Logger logger = LoggerFactory.getLogger(CouponApplicationTests.class);

    @Autowired
   private MockMvc mvc;

   @Test
   public void contextLoads() throws Exception {
        TbkDgItemCouponGetRequest req = new TbkDgItemCouponGetRequest();

        req.setCat("33");
        req.setPageSize(20L);
        req.setQ("裤子");
        req.setPageNo(4L);

        ResultActions resultActions =
                mvc.perform(MockMvcRequestBuilders.get("/couponGet.json?cat={cat}","33"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        resultActions.andDo(print());
        MvcResult mvcResult = resultActions.andReturn();
        logger.info("--------------------");
        logger.info(mvcResult.getResponse().getContentAsString());
        logger.info("--------------------");

    }



    @Test
    public void tbCategoryQuery() throws Exception {

        ResultActions actions =
                mvc.perform(MockMvcRequestBuilders.get("/tbCategoryList.json"));
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        MvcResult mvcResult = actions.andReturn();
        String str = mvcResult.getResponse().getContentAsString();
        logger.info("----------");
        logger.info(str);
    }

    @Test
    public void queryPlist() throws Exception{
        ResultActions actions =
                mvc.perform(MockMvcRequestBuilders.get("/tbCategoryPlist.json"));
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        MvcResult mvcResult = actions.andReturn();
        String str = mvcResult.getResponse().getContentAsString();
        logger.info("----------");
        logger.info(str);

    }
    @Test
    public void queryClist() throws Exception{
        ResultActions actions =
                mvc.perform(MockMvcRequestBuilders.get("/tbCategoryClist.json"));
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        MvcResult mvcResult = actions.andReturn();
        String str = mvcResult.getResponse().getContentAsString();
        logger.info("----------");
        logger.info(str);
    }

    @Test
    public void queryJoinlist() throws Exception{
        ResultActions actions =
                mvc.perform(MockMvcRequestBuilders.get("/tbCategoryJoinlist.json"));
        actions.andExpect(MockMvcResultMatchers.status().isOk());
        MvcResult mvcResult = actions.andReturn();
        String str = mvcResult.getResponse().getContentAsString();
        logger.info("----------");
        logger.info(str);
    }

}

猜你喜欢

转载自blog.csdn.net/lixia755324/article/details/80170131