zoukankan      html  css  js  c++  java
  • 使用Mock 测试 controller层

    package action;

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    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 org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;


    /**
    * Created by duanxinli on 2019/10/16.
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration()
    @ContextConfiguration({"classpath:application-context.xml","classpath:application-context-datasource.xml","classpath:springmvc-servlet.xml"})
    public class MVCTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;
    @Before
    public void init(){
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); //构造MockMvc
    }

    @Test
    public void getshortCutTest() throws Exception {
    ResultActions reaction=this.mockMvc.perform(MockMvcRequestBuilders.get("/shortcut/listAll")
    .accept(MediaType.APPLICATION_JSON));//返回值接收json
    reaction.andExpect(MockMvcResultMatchers.status().isOk());
    MvcResult mvcResult =reaction.andReturn();
    System.out.println(mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void postshortCutTest() throws Exception {
    ResultActions reaction =this.mockMvc.perform(MockMvcRequestBuilders.post("/policy/info/save")
    .contentType(MediaType.APPLICATION_JSON)//请求体时json
    .header("Timestamp", "1496656373791")
    .header("AppId", "1003"));
    reaction.andExpect(MockMvcResultMatchers.status().isOk());
    MvcResult mvcResult =reaction.andReturn();
    System.out.println(mvcResult.getResponse().getContentAsString());
    }

    }
  • 相关阅读:
    c++ map 的基本操作
    hdu Dragon Balls
    hdu Code Lock
    小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
    将.py脚本打包成.exe
    关于IO同步/异步/阻塞/非阻塞文章
    C++文件操作
    (十)参考教程
    (八)树控件(Tree Control),标签控件(tab control)
    (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static
  • 原文地址:https://www.cnblogs.com/duan2/p/11704310.html
Copyright © 2011-2022 走看看