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());
	}
	
}

 

 

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页