zoukankan      html  css  js  c++  java
  • 使用Spring MockMVC对controller做单元测试

    1、对单一controller做测试。

    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    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.setup.MockMvcBuilders;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration("classpath:")
    @ContextConfiguration("/data/spring-test.xml")
    public class CommonCtrlTest {
    
        private MockMvc mockMvc;
    
        @Autowired
        CommonCtrl commonCtrl;
    
        @Before
        public void setup() {
            this.mockMvc = MockMvcBuilders.standaloneSetup(commonCtrl).build();
        }
    
        @Test
        public void testHello() throws Exception {
            ResultActions resultActions = this.mockMvc.perform(
                    MockMvcRequestBuilders.get("/test").accept(MediaType.APPLICATION_JSON));
            MvcResult mvcResult = resultActions.andReturn();
            String result = mvcResult.getResponse().getContentAsString();
            System.out.println("response:" + result);
        }
    }

    2、对整个环境做测试,包括Interceptor。

    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;
    import outfox.course.weixinkaoshen.dao.AbstractTest;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    public class TestKaoshenCtrl extends AbstractTest{
        @Autowired
        private WebApplicationContext wac;
    
        private MockMvc mockMvc;
    
        @Before
        public void setUp() {
            mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }
    
        @Test
        public void testAddUser() throws Exception {
            mockMvc.perform((get("/test/get").param("id", "1")))
                    .andExpect(status().isOk()).andDo(print());
        }
    }
  • 相关阅读:
    Infopath Notify 弹出提示信息
    window.showModalDialog 返回值
    【转】获得正文内容中的所有img标签的图片路径
    Json Datable Convert
    Sharepoint 列表 附件 小功能
    Surgey 权限更改
    SQL 触发器用于IP记录转换
    Caml语句 查询分配给当前用户及当前组
    jquery 1.3.2 auto referenced when new web application in VSTS2010(DEV10)
    TFS diff/merge configuration
  • 原文地址:https://www.cnblogs.com/ylty/p/6420738.html
Copyright © 2011-2022 走看看