本篇内容主要讲解“junit单元测试事务自动回滚的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“junit单元测试事务自动回滚的方法”吧!
junit 单元测试事务会自动回滚。 通过@Rollback(true)注解来实现,默认是true,事务会回滚,可以不写。false时事务不会回滚,数据会写到数据库中。
package com.xiaolyuh.service;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.xiaolyuh.entity.RewardCouponDetail;
import com.xiaolyuh.mapper.RewardCouponDetailMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RewardCouponDetailServiceTest{
private static final Logger logger = LoggerFactory.getLogger(RewardCouponDetailServiceTest.class);
@Autowired
private RewardCouponDetailMapper rewardCouponDetailMapper;
@Test
@Transactional
@Rollback(true)// 事务自动回滚,默认是true。可以不写
public void TestCustomer(){
RewardCouponDetail rewardCouponDetail = new RewardCouponDetail();
rewardCouponDetail.setRewardInfoId(1L);
rewardCouponDetail.setCouponId("1");
rewardCouponDetail.setCouponDetailId(1L);
rewardCouponDetail.setCreateTime(new Date());
rewardCouponDetailMapper.insert(rewardCouponDetail);
logger.info(JSON.toJSONString(rewardCouponDetail));
Assert.assertNotNull(rewardCouponDetail.getId());
}
}
到此,相信大家对“junit单元测试事务自动回滚的方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/xiaolyuh/blog/3116841