Spring Boot 单元测试学习(一)

本文介绍了如何在SpringBoot项目中使用单元测试,特别是通过MockMvc进行接口测试。通过示例展示了如何创建和执行POST请求,并验证响应状态。测试过程中,利用@Transactional注解确保数据库操作在测试后自动回滚,保持数据一致性。
摘要由CSDN通过智能技术生成

本文主要是记录在 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值