SpringBoot使用Junit测试 防止事物自动回滚
问题:我在测试类中的save方法测试成功通过,但数据库没有插入数据
测试方法如下:
-
@Test
-
@Transactional
-
// @Rollback(false)
-
public void saveOne(){
-
ProductCategory productCategory = new ProductCategory();
-
productCategory.setCategoryName("优惠单品");
-
productCategory.setCategoryType(3);
-
ProductCategory productCategory1 = productCategoryRepository.saveAndFlush(productCategory);
-
log.info(productCategory1.toString());
-
}
执行测试方法后,数据库没有插入数据
原因:springboot中使用junit编写单元测试,@Transactional默认是事物回滚的,这样测试的脏数据不影响数据库。具体看控制台输出也能看来:
所以这里为了验证测试方法save是否正确应该关闭自动事物回滚,使用@Rollback(false)关闭回滚。
-
@Test
-
@Transactional
-
@Rollback(false)
-
public void saveOne(){
-
ProductCategory productCategory = new ProductCategory();
-
productCategory.setCategoryName("优惠单品");
-
productCategory.setCategoryType(3);
-
ProductCategory productCategory1 = productCategoryRepository.saveAndFlush(productCategory);
-
log.info(productCategory1.toString());
-
}
运行测试方法,控制台输出如下:
数据库也插入了对应的数据。最后吐槽一下,Spring Data JPA这破玩意真不好用,底层还是Hibernate,各种坑吧!
原文地址:https://blog.csdn.net/PORSCHE_GT3RS/article/details/81417287
SpringBoot使用Junit测试 防止事物自动回滚的更多相关文章
- junit测试类防止事务回滚-工作心得
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 12 月 06 日 原地址:https://niaobulashi.com/archives/junit-test-rollb ...
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
- flask-sqlalchemy、pytest 的单元测试和事务自动回滚
flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...
- mysql 事务中如果有sql语句出错,会导致自动回滚吗?
事务,我们都知道具有原子性,操作要么全部成功,要么全部失败.但是有可能会造成误解. 我们先准备一张表,来进行测试 CREATE TABLE `name` ( `id` int(11) unsigned ...
- 【linux】【jenkins】jenkins构建、mvn或者npm打包、docker运行、失败自动回滚脚本
小白对jenkins运维的使用有点简单的想法,这里开个记录贴记录下. 由于未找到jenkins构建失败后执行其他脚本的插件,也暂时没有使用其他运维工具.所以想自己写一个shell脚本,一是方便其他人使 ...
- MockMvc 进行 controller层单元测试 事务自动回滚 完整实例
package com.ieou.ms_backend.controller; import com.google.gson.Gson; import com.ieou.ms_backend.dto. ...
- spring事物不回滚的问题
学习spring分布式事务的时候,自己整了个demo,写test测试的时候发先事物没有回滚.此问题和分布式事务没关系.1.在service层引入@Transaction注解 使用 throw new ...
- 使用Jenkins结合Gogs和SonarQube对项目代码进行测试、部署、回滚,以及使用keepalived+haproxy调度至后端tomcat
0 环境说明 主tomcat:192.168.0.112 备tomcat:192.168.0.183 haproxy+keepalived-1:192.168.0.156 haproxy+keepal ...
- DDL, DML不是所有SQL都是可以自动回滚的
因为DDL没有事务性,所以DDL不能回滚. 要实现自动回滚.(begin,commit,rollback),则SQL语句中只能包括DML. 这样,自动化发布就会受限规范格式. 故而,一刀切的办法是,假 ...
随机推荐
- 【OI】倍增求LCA
╭(′▽`)╯ 总之,我们都知道lca是啥,不需要任何基础也能想出来怎么用最暴力的方法求LCA,也就是深度深的点先跳到深度浅的点的同一深度,然后一起向上一步步跳.这样显然太慢了! 所以我们要用倍增,倍 ...
- objectarx之画多段线和画直线
void CCommonFuntion::DrowPloyLine(AcGePoint2dArray& inputpoints){ if (inputpoints.length() < ...
- Person Re-identification 系列论文笔记(七):PCB+RPP
Beyond Part Models: Person Retrieval with Refined Part Pooling Sun Y, Zheng L, Yang Y, et al. Beyond ...
- 洛谷 P1119 灾后重建 最短路+Floyd算法
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 总结 题面 题目链接 P1119 灾后重建 题目描述 B地区在地震过后,所有村 ...
- JavaScript学习之setTimeout
<JavaScript权威指南>第四版中说“window对象方法setTimeout()用来安排一个JavaScript的代码段在将来的某个指定时间运行”. setTimeout(foo, ...
- 安装完django验证是否安装成功&&运行项目
1.验证是否成功安装django 上图证明安装成功 2.打开客户端的pycharm,在terminal中打开窗体,什么都没输入,运行下面的命令 python manage.py runserver 在 ...
- 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script©
大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块
- nodeJs学习-01 http模块
http模块基础: const http = require("http"); //引入http系统模块 var server = http.createServer(functi ...
- Xcode编译报错:< Apple Mach-O Linker Warning > clang: error: no such file or directory: 'xxxx'
Xcode编译报错概述: clang: error: no such file or directory: 'CoreGraphics' 一般原因是链接库内容导入丢失,这种的排查下target - B ...
- 亿级消息系统的核心存储:Tablestore发布Timeline 2.0模型
背景 互联网快速发展的今天,社交类应用.消息类功能大行其道,占据了大量网络流量.大至钉钉.微信.微博.知乎,小至各类App的推送通知,消息类功能几乎成为所有应用的标配.根据场景特点,我们可以将消息类场 ...