詳解springboot中junit回滾
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)于SpringBoot的異?;貪L和事務(wù)的使用詳解
- springboot使用mybatis開啟事務(wù)回滾
- SpringBoot數(shù)據(jù)層測(cè)試事務(wù)回滾的實(shí)現(xiàn)流程
- SpringBoot事務(wù)不回滾的解決方案
- 使用SpringBoot注解方式處理事務(wù)回滾實(shí)現(xiàn)
- SpringBoot事務(wù)使用及回滾實(shí)現(xiàn)代碼詳解
- springboot手動(dòng)事務(wù)回滾的實(shí)現(xiàn)代碼
- Spring Boot 各種回滾操作實(shí)戰(zhàn)教程(自動(dòng)回滾、手動(dòng)回滾、部分回滾)
相關(guān)文章
IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版)
這篇文章主要介紹了IDEA 自帶的數(shù)據(jù)庫(kù)工具真的很牛(收藏版),本文以 IntelliJ IDEA/ Mac 版本作為演示,其他版本的應(yīng)該也差距不大,需要的朋友可以參考下2021-04-04關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法
以下為大家介紹,關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法,希望對(duì)有需要的朋友有所幫助。2013-04-04Spring?Boot?+?Spring?Batch?實(shí)現(xiàn)批處理任務(wù)的詳細(xì)教程
這篇文章主要介紹了Spring?Boot+Spring?Batch實(shí)現(xiàn)批處理任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08java的Array,List和byte[],String相互轉(zhuǎn)換的方法你了解嘛
這篇文章主要為大家詳細(xì)介紹了java的Array,List和byte[],String相互轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié)(含多個(gè)實(shí)例)
jdk1.8的一些新特性簡(jiǎn)化了代碼的寫法,減少了部分開發(fā)量,下面這篇文章主要給大家介紹了關(guān)于JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié),文中包含了多個(gè)實(shí)例代碼,需要的朋友可以參考下2023-01-01java自己手動(dòng)控制kafka的offset操作
這篇文章主要介紹了java自己手動(dòng)控制kafka的offset操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Java中Date數(shù)據(jù)類型的數(shù)值轉(zhuǎn)換方式
這篇文章主要介紹了Java中Date數(shù)據(jù)類型的數(shù)值轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07