springboot中junit回滚的作用是什么-创新互联

这篇文章主要讲解了“springboot中junit回滚的作用是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot中junit回滚的作用是什么”吧!

创新互联10多年成都企业网站定制服务;为您提供网站建设,网站制作,网页设计及高端网站定制服务,成都企业网站定制及推广,对航空箱等多个行业拥有丰富的网站运维经验的网站建设公司。

springboot中使用junit编写单元测试,并且测试结果不影响数据库。


pom引入依赖

如果是IDE生成的项目,该包已经默认引入。



      org.springframework.boot
      spring-boot-starter-test
      test
    

数据库原始数据

springboot中junit回滚的作用是什么

原始数据


编写单元测试

package com.mos.quote;

import com.mos.quote.model.Area;
import com.mos.quote.service.IAreaService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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 java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QuoteApplicationTests {

  @Autowired
  private IAreaService areaService;

  @Test
  public void contextLoads() {
  }

  @Test
  public void testUpdate(){
    Area area = new Area();
    area.setCode("001003");
    area.setName("洛阳市");
    Integer result = areaService.update(area);
    Assert.assertEquals(1, (long)result);
  }

  @Test
  @Transactional
  @Rollback
  public void testUpdate4Rollback(){
    Area area = new Area();
    area.setCode("001001");
    area.setName("郑州市123");
    Integer result = areaService.update(area);
    Assert.assertEquals(1, (long)result);
  }

}

结果数据

springboot中junit回滚的作用是什么

结果数据


结论

可以看出code=001001的数据没有更改,而code=001003的数据修改成功。回头看代码:


@Transactional表示该方法整体为一个事务,


@Rollback表示事务执行完回滚,支持传入一个参数value,默认true即回滚,false不回滚。


该注解一样支持对类的注解,若如此做,对整个class的方法有效。

springboot中junit回滚的作用是什么

注解在class上

感谢各位的阅读,以上就是“springboot中junit回滚的作用是什么”的内容了,经过本文的学习后,相信大家对springboot中junit回滚的作用是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!


网站名称:springboot中junit回滚的作用是什么-创新互联
新闻来源:http://bzwzjz.com/article/epoed.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站设计制作公司 成都网站建设 成都网站设计 成都商城网站建设 成都网站建设 成都网站设计 企业网站建设公司 品牌网站建设 成都网站建设流程 成都网站建设 网站设计 成都商城网站建设 成都网站设计 外贸网站建设 重庆网站建设 高端网站设计 重庆网站建设 成都营销网站建设 网站设计公司 成都网站制作 四川成都网站建设 成都网站设计