小編給大家分享一下springboot中junit回滾有什么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創新互聯建站自2013年創立以來,我們提供高端網站建設、小程序定制開發、電商視覺設計、app軟件開發及網絡營銷搜索優化服務,在傳統互聯網與移動互聯網發展的背景下,我們堅守著用標準的設計方案與技術開發實力作基礎,以企業及品牌的互聯網商業目標為核心,為客戶打造具商業價值與用戶體驗的互聯網+產品。
springboot中使用junit編寫單元測試,并且測試結果不影響數據庫。
pom引入依賴
如果是IDE生成的項目,該包已經默認引入。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
數據庫原始數據
原始數據
編寫單元測試
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); } }
結果數據
結果數據
結論
可以看出code=001001的數據沒有更改,而code=001003的數據修改成功。回頭看代碼:
@Transactional表示該方法整體為一個事務,
@Rollback表示事務執行完回滾,支持傳入一個參數value,默認true即回滾,false不回滾。
該注解一樣支持對類的注解,若如此做,對整個class的方法有效。
注解在class上
看完了這篇文章,相信你對“springboot中junit回滾有什么用”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
分享文章:springboot中junit回滾有什么用
標題鏈接:http://m.newbst.com/article42/jeeoec.html
成都網站建設公司_創新互聯,為您提供網站改版、Google、品牌網站制作、網站收錄、動態網站、全網營銷推廣
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯