springBoot单元测试-模拟MVC测试

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

  略

 2)编写测试类 Application1TestMVC

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

  @AutoConfigureMockMvc // 注入MockMvc
 (当然你实在不想加也行,有其他办法 , 不过我不想说,麻烦)
  1 package com.cx.springboot;
  2 
  3 import java.util.Date;
  4 
  5 import org.junit.Test;
  6 import org.junit.runner.RunWith;
  7 import org.springframework.beans.factory.annotation.Autowired;
  8 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  9 import org.springframework.boot.test.context.SpringBootTest;
 10 import org.springframework.http.MediaType;
 11 import org.springframework.mock.web.MockHttpServletResponse;
 12 import org.springframework.test.context.junit4.SpringRunner;
 13 import org.springframework.test.web.servlet.MockMvc;
 14 import org.springframework.test.web.servlet.MvcResult;
 15 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 16 
 17 import com.alibaba.fastjson.JSON;
 18 import com.cx.springboot.hello1.model.UserModel;
 19 
 20 @RunWith(SpringRunner.class)
 21 @SpringBootTest
 22 @AutoConfigureMockMvc // 注入MockMvc
 23 public class Application1TestMVC {
 24 
 25     @Autowired
 26     private MockMvc mvc;
 27 
 28     /**
 29      * 
 30      * @throws Exception
 31      * @创建时间 2018年7月13日
 32      * @功能描述  通过链接传值 , 接受string 返回值   
 33      */
 34     @Test
 35     public void testString() throws Exception {
 36         //准备请求url  不用带ip、端口、项目名称等 直接写接口的映射地址就可以了
 37         String url = "/app/get2/zhangsan/1";
 38         
 39         /* 构建request 发送请求GET请求
 40          * MockMvcRequestBuilders 中有很多 请求方式。像get、post、put、delete等等
 41          */
 42         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(url)
 43                 .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
 44                 .andReturn();// 得到返回结果
 45         
 46         MockHttpServletResponse response = mvcResult.getResponse();
 47         //拿到请求返回码
 48         int status = response.getStatus();
 49         //拿到结果
 50         String contentAsString = response.getContentAsString();
 51 
 52         System.err.println(status);
 53         System.err.println(contentAsString);
 54     }
 55 
 56     
 57     
 58     /**
 59      * 
 60      * @throws Exception
 61      * @创建时间 2018年7月13日
 62      * @功能描述  传递header ,接受 返回值   
 63      */
 64     @Test
 65     public void headerTest() throws Exception {
 66         // uri
 67         String uri = "/app/get4";
 68         
 69         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri)
 70                 .header("token", "asd123")
 71                 .header("name", "zhangsan11")
 72                 .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
 73                 .andReturn();// 得到返回结果
 74         
 75         MockHttpServletResponse response = mvcResult.getResponse();
 76         //拿到请求返回码
 77         int status = response.getStatus();
 78         //拿到结果
 79         String contentAsString = response.getContentAsString();
 80         
 81         System.err.println(status);
 82         System.err.println(contentAsString);
 83     }
 84     /**
 85      * 
 86      * @throws Exception
 87      * @创建时间 2018年7月13日
 88      * @功能描述  传递post请求和 bean类型对象 ,接受 返回值   
 89      */
 90     @Test
 91     public void postTest() throws Exception {
 92         // uri
 93         String uri = "/app/get3";
 94         
 95         UserModel userModel = new UserModel("张三", 11, new Date(), "abc123");
 96         
 97         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
 98                 .contentType(MediaType.APPLICATION_JSON_UTF8)
 99                 .content(JSON.toJSONString(userModel))
100                 .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json
101                 .andReturn();// 得到返回结果
102         
103         MockHttpServletResponse response = mvcResult.getResponse();
104         //拿到请求返回码
105         int status = response.getStatus();
106         //拿到结果
107         String contentAsString = response.getContentAsString();
108         
109         System.err.println(status);
110         System.err.println(contentAsString);
111     }
112 }
原文地址:https://www.cnblogs.com/cx987514451/p/9304525.html