一、模拟http请求测试接口

新建一个controller

@RestController
public class AATestController {
@GetMapping("/test")
public String test(@RequestParam("msg") String msg) {
return "返回消息:" + msg;
}
}
@SpringBootTest(classes = ApplicationTest.class)
@ExtendWith(SpringExtension.class)
public class AAControllerTest {
@Autowired
private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Test
void aa() throws Exception {
String url = "/test";
String msg = "模拟HTTP请求";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(url).accept(MediaType.APPLICATION_JSON).param("msg",msg);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
MvcResult mvcResult = mockMvc.perform(builder).andReturn();
int status = mvcResult.getResponse().getStatus();
if (status==200){
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
System.out.println("contentAsString = " + contentAsString);
}
}
}

最新文章

  1. 【leetcode】triangle(easy)
  2. 阿里云上安装vsftp笔记
  3. cookie和session区别
  4. devi into python 笔记(六)正则表达式 原始字符串
  5. C语言-switch语句
  6. CenoOS 7环境下编译OpenJDK8
  7. 前端 - jquery方式 / iframe +form 方式 上传文件
  8. 【bzoj5072】[Lydsy十月月赛]小A的树 树形背包dp
  9. Unity琐碎(1) 编辑器参数修改
  10. Xshell配置是vi显示多种颜色
  11. Go语言之高级篇Beego框架之爬虫项目实战
  12. RFBnet论文笔记
  13. C#-VS支持的语言
  14. NodeJS 笔记 URL模块
  15. [转载]使用 NuGet 管理项目库
  16. 修改windows默认的远程连接端口
  17. UVa 10817 校长的烦恼
  18. Tomcat启动 Unable to process Jar entry [javassist/XXXXXX.class]
  19. Android 画闹钟
  20. Spring Boot 整合 Thymeleaf 完整 Web 案例

热门文章

  1. Java学习笔记:2022年1月8日
  2. 腾讯出品小程序自动化测试框架【Minium】系列(三)元素定位详解
  3. springboot使用EasyExcel,导出数据到Excel表格,并且将Excel表中数据导入
  4. Vue15 v-for和key的作用及原理
  5. Dao包 对数据库的操作
  6. 题解 [AHOI2009]同类分布
  7. FreeFileSync:开源的文件同步工具 | Linux 中国
  8. 2022年冬pat乙级考试题目及代码解析附部分错误原因
  9. Cesium渲染调度
  10. 爆肝200+小时,总结出的 Creator 3.x 入门修炼指南!全免费