网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springboot中junit回滚有什么用

小编给大家分享一下springboot中junit回滚有什么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

创新互联建站自2013年创立以来,我们提供高端网站建设、小程序定制开发、电商视觉设计、app软件开发及网络营销搜索优化服务,在传统互联网与移动互联网发展的背景下,我们坚守着用标准的设计方案与技术开发实力作基础,以企业及品牌的互联网商业目标为核心,为客户打造具商业价值与用户体验的互联网+产品。

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回滚有什么用
链接URL:http://bjjierui.cn/article/jeeoec.html

其他资讯