当前位置:网站首页>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;

}

其中有两段话较为关键:

  1. @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.

  2. 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 是可以完成测试回滚功能的。

原网站

版权声明
本文为[油条生煎]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_46261993/article/details/108155236