這篇文章主要講解了“springboot中junit回滾的作用是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springboot中junit回滾的作用是什么”吧!
創(chuàng)新互聯(lián)專業(yè)提供成都主機托管四川主機托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機房位于中國電信/網(wǎng)通/移動機房,四川雅安電信機房服務(wù)有保障!
springboot中使用junit編寫單元測試,并且測試結(jié)果不影響數(shù)據(jù)庫。
pom引入依賴
如果是IDE生成的項目,該包已經(jīng)默認引入。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
數(shù)據(jù)庫原始數(shù)據(jù)
原始數(shù)據(jù)
編寫單元測試
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); } }
結(jié)果數(shù)據(jù)
結(jié)果數(shù)據(jù)
結(jié)論
可以看出code=001001的數(shù)據(jù)沒有更改,而code=001003的數(shù)據(jù)修改成功。回頭看代碼:
@Transactional表示該方法整體為一個事務(wù),
@Rollback表示事務(wù)執(zhí)行完回滾,支持傳入一個參數(shù)value,默認true即回滾,false不回滾。
該注解一樣支持對類的注解,若如此做,對整個class的方法有效。
注解在class上
感謝各位的閱讀,以上就是“springboot中junit回滾的作用是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對springboot中junit回滾的作用是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當(dāng)前名稱:springboot中junit回滾的作用是什么
文章網(wǎng)址:http://bm7419.com/article6/gipsog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、定制網(wǎng)站、Google、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)