zoukankan      html  css  js  c++  java
  • 单元测试中模拟mvc测试对象MockMvc

    import com.hengyu.chapter39.service.impl.MongoDBServiceImpl;
    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.MvcResult;
    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.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ChapterApplicationTests {
        @Autowired
        private MongoDBServiceImpl mongoDBService;
        /**
         * 模拟mvc测试对象
         */
        private MockMvc mockMvc;
    
        /**
         * web项目上下文
         */
        @Autowired
        private WebApplicationContext webApplicationContext;
    
        /**
         * 所有测试方法执行之前执行该方法
         */
        @Before
        public void before() {
            //获取mockmvc对象实例
            mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        }
    
        /**
         * 测试添加商品
         * @throws Exception
         */
        @Test
        public void addGood() throws Exception
        {
            MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/good/save")
                    .param("name","西红柿")
                    .param("unit","斤")
                    .param("price","12.88")
            )
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.status().is(200))
                    .andReturn();
            result.getResponse().setCharacterEncoding("UTF-8");
            System.out.println(result.getResponse().getContentAsString());
        }
    
        @Test
        public void queryMongo() throws Exception {
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/template/query"))
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.status().is(200))
                    .andReturn();
            System.out.println(mvcResult.getResponse().getContentAsString());
        }
    
        @Test
        public void list() throws Exception {
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/template/list"))
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.status().is(200))
                    .andReturn();
            mvcResult.getResponse().setCharacterEncoding("utf8");
            System.out.println(mvcResult.getResponse().getContentAsString());
        }
    }
  • 相关阅读:
    Mac电脑maven安装与配置
    解决Mac OS X中IDEA启动慢以及debug卡死问题
    如何在Mac上启用root用户或更改root密码
    mac文本编辑器软件,五大适用于Mac修订的文本编辑器,nodepad++替代软件
    mac系统到10.14以上,navicat无法打开,一直显示已损坏解决办法
    mac苹果电脑AppleID注册或者登录appstore时提示:您没有完整填写表格,请输入您的出生年份的解决方法
    mac苹果电脑使用相关,开发环境配置指南(持续更新)
    bitmap to base64
    Multiple actions were found that match the request in Web Api
    vue get attribute value
  • 原文地址:https://www.cnblogs.com/liangmm/p/12028706.html
Copyright © 2011-2022 走看看