What is the function of junit rollback in springboot
This article mainly explains "what is the role of junit rollback in springboot". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of junit rollback in springboot".
Junit is used in springboot to write unit tests, and the test results do not affect the database.
Pom introduces dependency
If it is a project generated by IDE, the package has been introduced by default.
Org.springframework.boot spring-boot-starter-test test
Database raw data
raw data
Write unit tests
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) @ SpringBootTestpublic class QuoteApplicationTests {@ Autowired private IAreaService areaService; @ Testpublic void contextLoads () {} @ Testpublic void testUpdate () {Area area = new Area (); area.setCode ("001003"); area.setName ("Luoyang"); 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 ("Zhengzhou 123"); Integer result = areaService.update (area); Assert.assertEquals (1, (long) result);}
Result data
Result data
Conclusion
You can see that the data in code=001001 has not changed, while the data in code=001003 has been modified successfully. Look back at the code:
@ Transactional indicates that the method as a whole is a transaction
@ Rollback means that the transaction is rolled back. You can pass a parameter value. By default, true is rolled back, and false does not roll back.
This annotation also supports annotations to the class, which, if so, is valid for the entire class method.
Comment on class
Thank you for your reading, the above is the content of "what is the role of junit rollback in springboot". After the study of this article, I believe you have a deeper understanding of what the role of junit rollback in springboot is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!