Springboot项目中Controller层的单元测试

server/2024/10/21 19:40:00/

源码展示:
原来的controller类:

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/web")
@Slf4j
@Service
public class WebController {@RequestMapping("/get")public String get(){log.info("get");return "get";}@PostMapping("/post")public String post(@RequestBody Student student){log.info("post method student={}",student );return "post success";}@PostMapping("/postHead")public String postHead(@RequestHeader("token") String token){log.info("token : "+token);return "post head token:"+token;}}

自己定义的一个测试对象student:

import lombok.Data;@Data
public class Student {private String name;private int age;
}

测试代码:

package org.qjg.controller;import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;@Slf4j
@SpringBootTest
class WebControllerTest {private MockMvc mockMvc;private HttpHeaders headers;@Resourceprivate WebApplicationContext webApplicationContext;private String baseUrl = "/web";@BeforeEachpublic void init() {mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//或者使用普通的map也是可以的map.add("token", "qjg-token");headers = new HttpHeaders();headers.putAll(map);;}@SneakyThrows@Testvoid get() {MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(baseUrl + "/get").headers(headers).contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);log.info("contentAsString: " + contentAsString);}@SneakyThrows@Testvoid post() {Student student = new Student();student.setAge(18);student.setName("张三");MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "/post").contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(student)).headers(headers)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);log.info("contentAsString: " + contentAsString);}@SneakyThrows@Testvoid postHead() {MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "/postHead").contentType(MediaType.APPLICATION_JSON).content("").headers(headers)).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);Assert.isTrue("post head token:qjg-token".equals(contentAsString));}
}

http://www.ppmy.cn/server/6108.html

相关文章

react中关于类式组件和函数组件对props、state、ref的使用

文章中有很多蓝色字体为扩展链接&#xff0c;可以补充查看。 常用命令使用规则 组件编写方式: 1.函数式 function MyButton() { //直接return 标签体return (<>……</>); }2.类 class MyButton extends React.Component { //在render方法中&#xff0c;return…

速盾:cdn可以加速哪些服务器

CDN&#xff08;Content Delivery Network&#xff0c;内容分发网络&#xff09;是一种通过将网站的静态资源部署到全球各地的服务器上&#xff0c;以提供更快速、更可靠的访问体验的技术。CDN可以加速许多类型的服务器&#xff0c;包括但不限于以下几种&#xff1a; 静态资源服…

抓取电商产品数据的方法|电商平台商品详情数据|批量上架|商品搬家|电商封装API数据采集接口更高效安全的数据采集

大量级电商数据采集时使用电商API接口有以下优势&#xff1a; 1. 数据准确性&#xff1a;通过电商API接口获取数据&#xff0c;可以保证数据的准确性和实时性&#xff0c;避免了手动采集可能出现的错误和延迟。 2. 自动化采集&#xff1a;API接口可以实现自动化的数据获取和更…

Flask + Bootstrap vs Flask + React/Vue:初学者指南

好的&#xff0c;让我为你提供一个初学者指南&#xff0c;并附上一些示例代码来说明 Flask Bootstrap 和 Flask React/Vue 的使用。 Flask Bootstrap&#xff1a; 安装 Flask 和 Bootstrap&#xff1a; 首先&#xff0c;确保你已经安装了 Python 和 pip。然后可以使用 pip …

【PHP快速上手(十四)】

目录 PHP快速上手&#xff08;十四&#xff09;PHP 中常用数据库操作使用 WHERE 子句进行条件查询使用 ORDER BY 子句进行排序使用 UPDATE 语句更新数据使用 DELETE 语句删除数据执行事务总结 PHP快速上手&#xff08;十四&#xff09; PHP 中常用数据库操作 当使用 PHP 中的…

C++|stack-queue-priority_queue(适配器+模拟实现+仿函数)

目录 一、容器适配器 1.1容器适配器概念的介绍 1.2stack和queue的底层结构 1.3deque容器的介绍 1.3.1deque的缺陷及为何选择他作为stack和queue的底层默认实现 二、stack的介绍和使用 2.1stack的介绍 2.2stack的使用 2.3stack的模拟实现 三、queue的介绍和使用 …

系统服务控制

系统服务控制 格式&#xff1a;systemctl 控制类型 服务名称 控制类型 start:启动stop:停止restart:重新启动reload:重新加载status :查看服务状态 例&#xff1a; systemctl status firewalld //显示防火墙状态 systemctl stop firewalld.service //关闭防火墙…

Spring AOP(面向切面编程)

1.Spring AOP 简介 1.1 AOP概述 AOP 为 Aspect Oriented Programming 的缩写&#xff0c;意思为面向切面编程, 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP 是 OOP 的延续&#xff0c;是Spring框架中的一个重要内容&#xff0c;是函数式编程的一…