欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解springboot中junit回滾

 更新時(shí)間:2017年10月30日 16:58:26   作者:夢(mèng)想修補(bǔ)師  
本篇文章主要介紹了springboot中junit回滾,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

springboot中使用junit編寫單元測(cè)試,并且測(cè)試結(jié)果不影響數(shù)據(jù)庫(kù)。

pom引入依賴

如果是IDE生成的項(xiàng)目,該包已經(jīng)默認(rèn)引入。

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

數(shù)據(jù)庫(kù)原始數(shù)據(jù)

原始數(shù)據(jù)

編寫單元測(cè)試

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("洛陽(yáng)市");
    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);
  }

}

結(jié)果數(shù)據(jù)

結(jié)果數(shù)據(jù)

結(jié)論

可以看出code=001001的數(shù)據(jù)沒(méi)有更改,而code=001003的數(shù)據(jù)修改成功?;仡^看代碼:

@Transactional表示該方法整體為一個(gè)事務(wù),

@Rollback表示事務(wù)執(zhí)行完回滾,支持傳入一個(gè)參數(shù)value,默認(rèn)true即回滾,false不回滾。

該注解一樣支持對(duì)類的注解,若如此做,對(duì)整個(gè)class的方法有效。

注解在class上

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論