Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of automatic rollback of junit Unit Test transaction

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the method of automatic rollback of junit unit test transactions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the method of automatic rollback of junit unit test transactions.

The junit unit test transaction is automatically rolled back. This is achieved through the @ Rollback (true) annotation, which defaults to true, and the transaction will be rolled back and may not be written. During false, the transaction is not rolled back and the data is written to the database.

Package com.xiaolyuh.service; import java.util.Date; import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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 com.alibaba.fastjson.JSON;import com.xiaolyuh.entity.RewardCouponDetail Import com.xiaolyuh.mapper.RewardCouponDetailMapper; @ RunWith (SpringRunner.class) @ SpringBootTestpublic class RewardCouponDetailServiceTest {private static final Logger logger = LoggerFactory.getLogger (RewardCouponDetailServiceTest.class); @ Autowired private RewardCouponDetailMapper rewardCouponDetailMapper; @ Test @ Transactional @ Rollback (true) / / transaction is automatically rolled back. The default is true. You can not write public void TestCustomer () {RewardCouponDetail rewardCouponDetail = new RewardCouponDetail (); rewardCouponDetail.setRewardInfoId (1L); rewardCouponDetail.setCouponId ("1"); rewardCouponDetail.setCouponDetailId (1L); rewardCouponDetail.setCreateTime (new Date ()); rewardCouponDetailMapper.insert (rewardCouponDetail); logger.info (JSON.toJSONString (rewardCouponDetail)) Assert.assertNotNull (rewardCouponDetail.getId ());}} at this point, I believe you have a better understanding of "the method of automatic rollback of junit unit test transactions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report