当前位置:网站首页>JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
2022-06-21 08:27:00 【油条生煎】
JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
笔者在学习 Spring Boot 测试(JUnit5)的测试回滚功能时,在网上找到的办法都是在方法上加 @Transactional 注解和 @Rollback 注解,但笔者在 2020/8/21 使用 Spring Boot 进行单元测试的时候,发现其已无 @Transactional 注解(宇宙第一 IDEA 未给出代码提示),而且只使用 @Rollback 注解就可以完成回滚操作。这难道是说 JUnit5 中已经不使用 @Transactional 注解了吗?
为了测试单独使用 @Rollback 是否可以达成事务回滚的效果,笔者写了以下的测试类:
/** 两个标有 @Test 的测试方法,会依照方法名开头字母(的ASCII码值)升序进行 */
@SpringBootTest
class CalculatorServiceTest {
private int a = 0; // a 的默认值为 0
@Test
@Rollback // 回滚注解
void test0() {
a = 1; // 将 a 的值修改为 1
// 在标记了 @Rollback 回滚注解的方法结束前,输出 a 的值
System.out.println("a = " + a);
}
@Test
void test1() {
// 输出 a 的值
System.out.println("a = " + a);
}
}
输出内容如下:
...(日志信息)
a = 1 // test0() 的输出结果
a = 0 // test1() 的输出结果
BUILD SUCCESSFUL in 2s
4 actionable tasks: 2 executed, 2 up-to-date
19:22:25: Task execution finished ':test --tests "xxx.yyyy.service.CalculatorServiceTest"'.
结果发现 a 在 test0() 方法中确实被修改为了 1,而且在 test0() 方法结束后确实也被修改回去了。
姑且推测其是 “回滚成功” 了。
再看向 @Rollback 的源码(省略了开头的版权说明 Copyright ):
package org.springframework.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** * {@code @Rollback} is a test annotation that is used to indicate whether * a <em>test-managed transaction</em> should be <em>rolled back</em> after * the test method has completed. * * <p>Consult the class-level Javadoc for * {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener} * for an explanation of <em>test-managed transactions</em>. * * <p>When declared as a class-level annotation, {@code @Rollback} defines * the default rollback semantics for all test methods within the test class * hierarchy. When declared as a method-level annotation, {@code @Rollback} * defines rollback semantics for the specific test method, potentially * overriding class-level default commit or rollback semantics. * * <p>As of Spring Framework 4.2, {@code @Commit} can be used as direct * replacement for {@code @Rollback(false)}. * * <p><strong>Warning</strong>: Declaring {@code @Commit} and {@code @Rollback} * on the same test method or on the same test class is unsupported and may * lead to unpredictable results. * * <p>This annotation may be used as a <em>meta-annotation</em> to create * custom <em>composed annotations</em>. Consult the source code for * {@link Commit @Commit} for a concrete example. * * @author Sam Brannen * @since 2.5 * @see Commit * @see org.springframework.test.context.transaction.TransactionalTestExecutionListener */
@Target({
ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Rollback {
/** * Whether the <em>test-managed transaction</em> should be rolled back * after the test method has completed. * <p>If {@code true}, the transaction will be rolled back; otherwise, * the transaction will be committed. * <p>Defaults to {@code true}. */
boolean value() default true;
}
其中有两段话较为关键:
@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.
Whether the test-managed transaction should be rolled back after the test method has completed.
If true, the transaction will be rolled back; otherwise, the transaction will be committed.
Defaults to true.
第一段话是注释在整个 @Rollback 注解类上的,大意为:@Rollback 用于在测试完成之后,回滚其所标记的类/方法。
第二段话是注释的 value() 上的,该属性表示 “是否开启回滚” ,默认值为 true(即回滚,即不提交)。
结论:只使用 @Rollback 是可以完成测试回滚功能的。
边栏推荐
- Redis master-slave vulnerability and remote connection vulnerability
- [actual combat] ACM players illustrate leetcode using stack to realize queue
- Images, graphics and Applications (II)
- 4.8 inquirer-autocomplete-prompt
- Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
- 声临其境 — 音频沉浸体验
- About SQL: field association between two tables
- 2.19 simulation summary
- 面试经验---字节
- 安装MySQL出现白页面怎么办
猜你喜欢

PS prompts "script error -50 general Photoshop error, how to solve it?

Linux Installation of Damon database /dm8 (with client tool installation full version)

解密FTP

Antd table how scroll bars appear in long tables

Mono of unity 5 can also support C # 6

Journal (résumé en langue c)

26. Hikvision camera configuration and preliminary test

Gql+nodejs+mysql database

Unity 5 自帶的Mono也可以支持C# 6

2022-2028 global cooling on-off valve industry research and trend analysis report
随机推荐
Linux安装达梦数据库/DM8(附带客户端工具安装完整版)
Mono of unity 5 can also support C # 6
Detailed introduction to PHP classes and objects
WordPress media library supports uploading and previewing SVG icons
Global and Chinese market for online automatic optical inspection 2022-2028: Research Report on technology, participants, trends, market size and share
给定一个m*n的二维列表,查找一个数是否存在
Can you implement these requirements with MySQL
CTF中命令执行知识点总结
Summary of mobile application development
Global and Chinese market of Toro from 2022 to 2028: Research Report on technology, participants, trends, market size and share
Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
Global and Chinese market for military imaging systems 2022-2028: Research Report on technology, participants, trends, market size and share
4.8 inquirer-autocomplete-prompt
Visual studio code annotation plug-in: korofileheader
Ads Filter Design Wizard tool 2
[kotlin] premier jour
[DB written interview 225] in Oracle, if the online redo log file is damaged, how to recover it?
[actual combat] ACM players illustrate leetcode using stack to realize queue
MySQL filter query (start with a letter, start with a number, start without a number, and start without a letter)
33 Jenkins modify plug-in source