激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解springboot中junit回滾

詳解springboot中junit回滾

2021-01-28 12:02夢想修補師 Java教程

本篇文章主要介紹了springboot中junit回滾,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

springboot中使用junit編寫單元測試,并且測試結果不影響數據庫。

pom引入依賴

如果是ide生成的項目,該包已經默認引入。

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

數據庫原始數據

詳解springboot中junit回滾

原始數據

編寫單元測試

?
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("洛陽市");
    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上

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩成人一区二区 | 最新av在线免费观看 | 日韩激情| 欧美成年性h版影视中文字幕 | 久久久av亚洲男天堂 | 成人在线免费观看网址 | 国产精品久久久久av | 综合在线一区 | 国产精品成人久久 | 日本欧美一区二区三区视频麻豆 | 91中文字幕在线观看 | 二级大黄大片高清在线视频 | 99riav国产在线观看 | 日韩在线观看免费 | 久久99综合 | 史上最强炼体老祖动漫在线观看 | 国产精品久久久久久久娇妻 | 关键词| 亚洲资源网 | 国产精品久久久久久久久久10秀 | 亚洲免费视频一区 | 国产精品自拍av | 欧美18一19sex性护士农村 | 色吧久久 | 色呦呦一区二区三区 | 无码专区aaaaaa免费视频 | 午夜视频在线免费播放 | 欧美极品免费 | 91亚洲精品一区二区福利 | 99热高清| 成人在线视频网 | 中文字幕在线永久 | 精品国产一区三区 | 91精品国产综合久久婷婷香 | 97超视频在线观看 | 国产在线看一区 | 免费一级毛片在线播放视频 | 视频一区 中文字幕 | 狠狠干b | 国产在线观看免费视频软件 | 国产午夜电影 |