Junit is used to write unit tests in springboot, and the test results do not affect the database.
Pom introduces dependencies
If it is a project generated by an IDE, this package has been introduced by default.
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
Database original data
Original data
Writing unit test
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); } }
Result Data
Result data
Conclusion
It can be seen that the data of code=001001 has not changed , and the data with code=001003 was modified successfully. Looking back at the code:
@Transactional indicates that the method as a whole is a transaction,
@Rollback indicates that the transaction is rolled back after execution. It supports passing in a parameter value, and the default is true. That is, rollback, false does not rollback.
This annotation also supports annotations on classes. If so, it will be effective for the entire class method.
Annotation on class
The above is the detailed content of What is the role of junit rollback in springboot. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools