最近做一个例子,按照老的项目进行搭建, Junit测试对数据库的插入和删除操作 ,测试用例运行时一切正常,新增方法执行正常。但是结果数据没有入库。
经过一段时间的分析发现是由于spring进行了回滚(查了INFO之后发现有rollback回滚操作),所以数据没有入库。解决方法是增加标签 @Rollback(value = false) 不要回滚
@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.setCreateTime(new Date()); rewardCouponDetailMapper.insert(rewardCouponDetail); logger.info(JSON.toJSONString(rewardCouponDetail)); Assert.assertNotNull(rewardCouponDetail.getId()); } }