junit使用进阶
2024-10-21 09:37:54
一、模拟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);
}
}
}
最新文章
- 【leetcode】triangle(easy)
- 阿里云上安装vsftp笔记
- cookie和session区别
- devi into python 笔记(六)正则表达式 原始字符串
- C语言-switch语句
- CenoOS 7环境下编译OpenJDK8
- 前端 - jquery方式 / iframe +form 方式 上传文件
- 【bzoj5072】[Lydsy十月月赛]小A的树 树形背包dp
- Unity琐碎(1) 编辑器参数修改
- Xshell配置是vi显示多种颜色
- Go语言之高级篇Beego框架之爬虫项目实战
- RFBnet论文笔记
- C#-VS支持的语言
- NodeJS 笔记 URL模块
- [转载]使用 NuGet 管理项目库
- 修改windows默认的远程连接端口
- UVa 10817 校长的烦恼
- Tomcat启动 Unable to process Jar entry [javassist/XXXXXX.class]
- Android 画闹钟
- Spring Boot 整合 Thymeleaf 完整 Web 案例
热门文章
- Java学习笔记:2022年1月8日
- 腾讯出品小程序自动化测试框架【Minium】系列(三)元素定位详解
- springboot使用EasyExcel,导出数据到Excel表格,并且将Excel表中数据导入
- Vue15 v-for和key的作用及原理
- Dao包 对数据库的操作
- 题解 [AHOI2009]同类分布
- FreeFileSync:开源的文件同步工具 | Linux 中国
- 2022年冬pat乙级考试题目及代码解析附部分错误原因
- Cesium渲染调度
- 爆肝200+小时,总结出的 Creator 3.x 入门修炼指南!全免费