日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springBoot单元测试-模拟MVC测试

發布時間:2025/4/9 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的springBoot单元测试-模拟MVC测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。