zoukankan      html  css  js  c++  java
  • springBoot单元测试-模拟MVC测试

    1)模拟mvc测试,和基础测试是一样的, 都需要在pom文件中引入junit的支持。

      略

     2)编写测试类 Application1TestMVC

      在类头上除啦加入之前的@RunWith(SpringRunner.class)、@RunWith(SpringRunner.class) 之外还要加入新的注解

      @AutoConfigureMockMvc // 注入MockMvc
     (当然你实在不想加也行,有其他办法 , 不过我不想说,麻烦)
    package com.cx.springboot;
    
    import java.util.Date;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    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.mock.web.MockHttpServletResponse;
    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 com.alibaba.fastjson.JSON;
    import com.cx.springboot.hello1.model.UserModel;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc // 注入MockMvc
    public class Application1TestMVC {
    
        @Autowired
        private MockMvc mvc;
    
        /**
         * 
         * @throws Exception
         * @创建时间 2018年7月13日
         * @功能描述  通过链接传值 , 接受string 返回值   
         */
        @Test
        public void testString() throws Exception {
            //准备请求url  不用带ip、端口、项目名称等 直接写接口的映射地址就可以了
            String url = "/app/get2/zhangsan/1";
            
            /* 构建request 发送请求GET请求
             * MockMvcRequestBuilders 中有很多 请求方式。像get、post、put、delete等等
             */
            MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)
                    .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
                    .andReturn();// 得到返回结果
            
            MockHttpServletResponse response = mvcResult.getResponse();
            //拿到请求返回码
            int status = response.getStatus();
            //拿到结果
            String contentAsString = response.getContentAsString();
    
            System.err.println(status);
            System.err.println(contentAsString);
        }
    
        
        
        /**
         * 
         * @throws Exception
         * @创建时间 2018年7月13日
         * @功能描述  传递header ,接受 返回值   
         */
        @Test
        public void headerTest() throws Exception {
            // uri
            String uri = "/app/get4";
            
            MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
                    .header("token", "asd123")
                    .header("name", "zhangsan11")
                    .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
                    .andReturn();// 得到返回结果
            
            MockHttpServletResponse response = mvcResult.getResponse();
            //拿到请求返回码
            int status = response.getStatus();
            //拿到结果
            String contentAsString = response.getContentAsString();
            
            System.err.println(status);
            System.err.println(contentAsString);
        }
        /**
         * 
         * @throws Exception
         * @创建时间 2018年7月13日
         * @功能描述  传递post请求和 bean类型对象 ,接受 返回值   
         */
        @Test
        public void postTest() throws Exception {
            // uri
            String uri = "/app/get3";
            
            UserModel userModel = new UserModel("张三", 11, new Date(), "abc123");
            
            MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
                    .contentType(MediaType.APPLICATION_JSON_UTF8)
                    .content(JSON.toJSONString(userModel))
                    .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
                    .andReturn();// 得到返回结果
            
            MockHttpServletResponse response = mvcResult.getResponse();
            //拿到请求返回码
            int status = response.getStatus();
            //拿到结果
            String contentAsString = response.getContentAsString();
            
            System.err.println(status);
            System.err.println(contentAsString);
        }
    }
  • 相关阅读:
    css 实现的纸张卷曲效果
    前端如何优化代码&前端web安全
    React native
    君士坦丁堡分叉引起的安全问题
    不用外部插件启用u盘ntfs写功能
    使用ubuntu搭建时间机器备份服务
    从一起“盗币”事件再谈合约安全问题
    如何让你的项目同时支持go vendor和go module
    golang plugin的依赖问题
    Plasma Cash合约解读
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/9845231.html
Copyright © 2011-2022 走看看