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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM