在单元测试中 Transaction注解会对数据库的更新(insert update delete)进行回滚 ,以保证数据不污染
Junit单元测试中,测试方法默认自动回滚,若要关闭自动回滚,需要使用注解@Rollback(false)显式关闭,默认回滚式@Rollback(true)
@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());
}
}