一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 詳解springboot中junit回滾

詳解springboot中junit回滾

2021-01-28 12:02夢(mèng)想修補(bǔ)師 Java教程

本篇文章主要介紹了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)引入。

?
1
2
3
4
5
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-test</artifactid>
  <scope>test</scope>
</dependency>

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

詳解springboot中junit回滾

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

編寫單元測(cè)試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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ù)

詳解springboot中junit回滾

結(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的方法有效。

詳解springboot中junit回滾

注解在class上

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

原文鏈接:http://www.jianshu.com/p/d9d0abf317c0?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 18无删减羞羞网站动漫 | 好 舒服 好 粗 好硬 好爽 | sex5·性屋娱乐| 欧美肥胖老妇做爰变态 | 日本老妇和子乱视频 | 特级淫片欧美高清视频蜜桃 | 亚洲 欧美 国产 日韩 字幕 | 男人的天堂在线观看入口 | 日韩亚洲欧美综合一区二区三区 | 草莓香蕉榴莲丝瓜秋葵绿巨人在线看 | 日本护士xxxx视频 | oneday日本在线观看完整版 | 精品午夜中文字幕熟女人妻在线 | 日韩国产成人资源精品视频 | 美女隐私部位视频网站 | 妇伦小说 | 欧美午夜视频一区二区 | 男男playh片在线观看 | 扒开双腿猛进入爽爽视频ai | 桥本有菜作品在线 | 逼逼流水了 | 国产精品永久免费自在线观看 | 欧美日本一本线在线观看 | 成人青青草 | hd最新国产人妖ts视频 | 97国产影院 | 亚洲国产精品嫩草影院久久 | 国产黄色大片网站 | 亚洲精品第三页 | 特黄特色大片免费影院 | 青青草原在线免费 | 成人先锋 | 黄色aaa级片 | 欧美二区三区 | 糖心vlog麻豆精东影业传媒 | 男男同志videos | 四虎成人影院 | 色偷偷亚洲综合网亚洲 | 毛片群 | 国产无限免费观看黄网站 | 99这里只有精品在线 |