本文主要是记录在 Spring Boot 项目中如何简单使用单元测试,对于相关的原理及知识暂不介绍。行文仓促,若有不妥之处,望各位大佬不吝赐教。
文章目录
一、搭建 Spring Boot 项目
这里展示部分代码,注意所用的数据库表(user)含有 user_id、user_name 两个字段。
- UserController.java
package com.example.study.unitteststudy.controller;
import com.example.study.unitteststudy.domain.User;
import com.example.study.unitteststudy.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author yi
* @date 2021/9/13 19:54
*/
@RestController
public class UserController {
private final UserService userService;
@Autowired
public UserController(

本文介绍了如何在SpringBoot项目中使用单元测试,特别是通过MockMvc进行接口测试。通过示例展示了如何创建和执行POST请求,并验证响应状态。测试过程中,利用@Transactional注解确保数据库操作在测试后自动回滚,保持数据一致性。
&spm=1001.2101.3001.5002&articleId=120275703&d=1&t=3&u=8cac50c9530c4057a258378d3e6f5dbb)
3万+

被折叠的 条评论
为什么被折叠?



