spring boot 单元测试JUnit5使用MockMvc调用get/post接口
源码地址:https://gitcode.net/qq_39339588/springboot.git
1. 先准备一份controller,一会儿供测试调用
package space.goldchen.springboot.test;
import org.springframework.web.bind.annotation.*;
import space.goldchen.springboot.entity.User;
/**
* 使用mockMvc 调用get/post请求地址
* @author chenzhao
* @create 2023-05-29 16:33
*/
@RestController
@RequestMapping("/mvcTest")
public class MvcTestController {
/**
* get请求接口
* @return
*/
@GetMapping
public String testGet(){
return "get";
}
/**
* post请求接口
* @param user
* @return
*/
@PostMapping
public User testPost(@RequestBody User user){
return user;
}
/**
* getById 请求
* @param id
* @return
*/
@GetMapping("byId")
public String testGetById(Integer id){
return "get:"+id;
}
}
2. MockMvc测试调用get请求接口
两个注解说明
@SpringBootTest // 加测试类上,标明是测试的类
@AutoConfigureMockMvc // 支持对MockMvc对象的注入和配置,测试get/post请求
测试get请求,传参json,添加请求头header,打印请求和响应
package space.goldchen.springboot.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import space.goldchen.springboot.entity.User;
import javax.annotation.Resource;
/**
* 单元测试:测试调用controller接口请求
*
* @author chenzhao
* @create 2023-05-29 16:37
*/
@SpringBootTest
//不用启动项目也可以调用MockMvc测试get/post请求
@AutoConfigureMockMvc
class MvcTestControllerTest {
@Resource
private MockMvc mockMvc;
@Resource
private ObjectMapper objectMapper;
/**
* 测试get请求,传参json,添加请求头header,打印请求和响应
*
* @throws Exception
*/
@Test
void testGet() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/mvcTest")
// 可以添加请求头
.content("{\"username\":\"goldchen\",\"password\":\"123456\"}")
.header("Authorization", "Bearer ..."))
// .contentType(MediaType.APPLICATION_JSON)
// .content("2"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
3. MockMvc测试调用get带参数请求接口
测试get请求,地址栏传参和param传参都行,添加请求头header,获取响应中的String字符串字段,打印请求响应结果文章来源:https://www.toymoban.com/news/detail-625956.html
package space.goldchen.springboot.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import space.goldchen.springboot.entity.User;
import javax.annotation.Resource;
/**
* 单元测试:测试调用controller接口请求
*
* @author chenzhao
* @create 2023-05-29 16:37
*/
@SpringBootTest
//不用启动项目也可以调用MockMvc测试get/post请求
@AutoConfigureMockMvc
class MvcTestControllerTest {
@Resource
private MockMvc mockMvc;
@Resource
private ObjectMapper objectMapper;
/**
* 测试get请求,地址栏传参和param传参都行,添加请求头header,获取响应中的String字符串字段,打印请求响应结果
*
* @throws Exception
*/
@Test
void testGetById() throws Exception {
int id = 1;
mockMvc
//地址栏传参和param传参都行
.perform(MockMvcRequestBuilders.get("/mvcTest/byId?id=" + id)
.header("Authorization", "Bearer ...")
.param("id", "" + id)
.content("12")
)
.andExpect(MockMvcResultMatchers.status().isOk())
// 返回结果中取值,并且去比较
.andExpect(MockMvcResultMatchers.content().string("get:" + id))
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
4. MockMvc测试调用post请求接口
测试post请求,传参json,添加请求头header,获取响应中的json字段,打印请求响应结果文章来源地址https://www.toymoban.com/news/detail-625956.html
package space.goldchen.springboot.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import space.goldchen.springboot.entity.User;
import javax.annotation.Resource;
/**
* 单元测试:测试调用controller接口请求
*
* @author chenzhao
* @create 2023-05-29 16:37
*/
@SpringBootTest
//不用启动项目也可以调用MockMvc测试get/post请求
@AutoConfigureMockMvc
class MvcTestControllerTest {
@Resource
private MockMvc mockMvc;
@Resource
private ObjectMapper objectMapper;
/**
* 测试post请求,传参json,添加请求头header,获取响应中的json字段,打印请求响应结果
*
* @throws Exception
*/
@Test
void testPost() throws Exception {
// json数据封装
User user = new User();
user.setUsername("goldchen");
user.setPassword("123456");
String contentString = objectMapper.writeValueAsString(user);
// 接收处理结果
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/mvcTest")
.contentType(MediaType.APPLICATION_JSON)
// 也可以手写json
// .content("{\"username\":\"goldchen\",\"password\":\"123456\"}"))
.content(contentString))
.andExpect(MockMvcResultMatchers.status().isOk())
// 可以取出 json的字段值
.andExpect(MockMvcResultMatchers.jsonPath("$.username")
.value("goldchen"))
.andDo(MockMvcResultHandlers.print()).andReturn();
// 获取响应结果
MockHttpServletResponse response = mvcResult.getResponse();
// 打印作为字符串
System.out.println(response.getContentAsString());
}
}
到了这里,关于spring boot 单元测试JUnit5使用MockMvc调用get请求,post请求,设置head请求头,解析返回值json和字符串的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!