首页 > Java > java教程 > 正文

正确地单元测试Spring Retry组件:避免常见陷阱与最佳实践

心靈之曲
发布: 2025-12-12 14:15:42
原创
640人浏览过

正确地单元测试Spring Retry组件:避免常见陷阱与最佳实践

本文旨在指导开发者如何正确地单元测试spring retry功能,解决在spring测试环境中`@autowired`注入的bean为`null`的常见问题。文章将深入探讨测试系统(sut)与依赖项的区分、`argumentmatchers.any()`的正确用法,并提供一个经过优化的测试代码示例,确保spring retry机制能够被有效且准确地验证。

理解Spring Retry单元测试的核心挑战

在Spring框架中,使用@Retryable注解的组件进行单元测试时,开发者常会遇到@Autowired注入的Bean为null的问题。这通常源于对测试策略、被测系统(SUT)与其依赖项的混淆,以及Mockito参数匹配器any()的误用。本教程将详细解析这些问题,并提供一套专业的解决方案。

1. 明确被测系统(SUT)与依赖项

进行单元测试时,首先要明确哪个是你的被测系统(System Under Test, SUT),以及哪些是SUT的依赖项。SUT是你想要验证其行为的类或方法,而依赖项是SUT为了完成其功能所需要协作的其他对象。

常见错误: 尝试模拟SUT本身。 当deltaHelper是你的SUT时,对其进行模拟(mock)会阻止你测试其真实逻辑。如果你模拟了deltaHelper,你实际上是在测试这个模拟对象,而不是DeltaHelper类的实际行为,包括其内部的@Retryable逻辑。

正确做法: 模拟SUT的依赖项。 为了控制SUT的行为路径(例如,让@Retryable方法触发重试),你应该模拟SUT的依赖项,并设置这些模拟对象的行为。例如,如果DeltaHelper依赖于MyRestService,那么你应该模拟MyRestService,并让它的方法抛出异常,从而触发DeltaHelper中的重试逻辑。

2. 正确使用ArgumentMatchers.any()

ArgumentMatchers.any()是Mockito提供的一个强大工具,用于在设置模拟行为(when)或验证调用(verify)时匹配任何参数。然而,它有一个关键的误区:any()方法在被调用时,会无条件地返回null

public static <T> T any() {
    reportMatcher(Any.ANY);
    return null; // 注意这里:它返回null
}
登录后复制

常见错误: 在实际的方法调用(“act”阶段)中,将any()作为参数传递给SUT。 例如,deltaHelper.process(any(), any())。由于any()返回null,这意味着你实际上是用null值来调用deltaHelper.process(null, null)。如果SUT的方法不处理null参数,这可能导致NullPointerException或其他非预期行为,而不是你期望的参数匹配。

正确做法: 仅在设置模拟行为或验证调用时使用any()。 在调用SUT的实际方法时,应该传递真实的、具体的参数值。

// 错误示例 (在调用SUT时使用any())
deltaHelper.process(any(), any()); // 实际调用的是 deltaHelper.process(null, null);

// 正确示例 (在设置mock行为时使用any())
when(mockRestService.call(any(), any())).thenThrow(new RuntimeException());

// 正确示例 (在验证mock调用时使用any())
verify(mockRestService, times(2)).call(any(), any());

// 正确示例 (在调用SUT时使用真实参数)
String apiArg = "testApi";
HttpEntity<?> entityArg = new HttpEntity<>("testBody");
deltaHelper.process(apiArg, entityArg);
登录后复制

优化Spring Retry单元测试示例

结合上述原则,以下是一个针对DeltaHelper类进行Spring Retry单元测试的优化示例。此示例使用了@MockBean来替换Spring上下文中的真实依赖,从而实现对MyRestService的模拟。

Pebblely
Pebblely

AI产品图精美背景添加

Pebblely 96
查看详情 Pebblely

前提: 假设DeltaHelper、MyStorageService是Spring组件(例如,带有@Component或@Service注解),MyRestService是一个简单的RestTemplate扩展类。

// DeltaHelper.java (SUT)
@Component
public class DeltaHelper {

    @Autowired
    MyRestService restService;

    @Autowired
    MyStorageService myStorageService;

    @NotNull
    @Retryable(
            value = Exception.class,
            maxAttemptsExpression = "${delta.process.retries}" // 从属性文件读取重试次数
    )
    public String process(String api, HttpEntity<?> entity) {
        System.out.println("Attempting to call restService for API: " + api);
        return restService.call(api, entity);
    }

    @Recover
    public String recover(Exception e, String api, HttpEntity<?> entity) {
        System.out.println("Recovering from exception: " + e.getMessage() + " for API: " + api);
        myStorageService.save(api);
        return "recover_success";
    }
}

// MyRestService.java (Dependency)
public class MyRestService extends org.springframework.web.client.RestTemplate {
    public String call(String api, HttpEntity<?> entity) {
        // 模拟实际的REST调用
        System.out.println("MyRestService.call invoked for API: " + api);
        // 实际应用中会进行网络请求
        return "real_response_from_" + api;
    }
}

// MyStorageService.java (Another Dependency)
@Service
public class MyStorageService {
    @Autowired
    MyRepo myRepo;

    @Async
    public MyEntity save(String api) {
        System.out.println("MyStorageService.save invoked for API: " + api);
        return myRepo.save(new MyEntity(api, System.currentTimeMillis()));
    }
}

// MyRepo.java (Simple interface/class for demonstration)
public interface MyRepo {
    MyEntity save(MyEntity entity);
}

public class MyEntity {
    private String api;
    private Long timestamp;
    public MyEntity(String api, Long timestamp) {
        this.api = api;
        this.timestamp = timestamp;
    }
    // Getters and Setters
}
登录后复制

优化后的测试类:

import org.junit.After;
import org.junit.Before;
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.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpEntity;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.validateMockitoUsage;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DeltaHelperTest.TestConfig.class) // 使用 @SpringBootTest 加载测试配置
public class DeltaHelperTest {

    @Autowired
    private DeltaHelper deltaHelper; // SUT: 真实实例

    @MockBean
    private MyRestService mockRestService; // 依赖: 模拟实例

    @MockBean
    private MyStorageService mockMyStorageService; // 依赖: 模拟实例,用于验证recover方法

    @MockBean
    private MyRepo mockMyRepo; // 依赖: 模拟实例,如果MyStorageService的save方法需要mock MyRepo

    @Before
    public void setUp() {
        // 设置重试次数的系统属性,确保@Retryable的maxAttemptsExpression能正确解析
        System.setProperty("delta.process.retries", "2");
        // 清除mock的调用历史,防止测试之间互相影响
        reset(mockRestService, mockMyStorageService, mockMyRepo);
    }

    @After
    public void validate() {
        // 验证所有mock对象是否有未验证的交互
        validateMockitoUsage();
    }

    @Test
    public void retriesAfterOneFailAndThenPass() throws Exception {
        // 1. 设置mockRestService的行为:
        // 第一次调用抛出异常 (触发重试)
        // 第二次调用返回成功 (重试成功)
        when(mockRestService.call(any(String.class), any(HttpEntity.class)))
                .thenThrow(new RuntimeException("Simulated network error"))
                .thenReturn("successful_response");

        // 2. 调用SUT的实际方法,并传入真实的参数
        String apiArg = "testApi";
        HttpEntity<?> entityArg = new HttpEntity<>("testBody");
        String result = deltaHelper.process(apiArg, entityArg);

        // 3. 验证结果和交互
        // 验证mockRestService的call方法被调用了2次
        verify(mockRestService, times(2)).call(apiArg, entityArg);
        // 验证最终结果
        assertThat(result).isEqualTo("successful_response");
        // 验证recover方法没有被调用,因为重试成功了
        verify(mockMyStorageService, never()).save(any(String.class));
    }

    @Test
    public void retriesFailAndThenRecover() throws Exception {
        // 1. 设置mockRestService的行为:
        // 第一次和第二次调用都抛出异常 (达到最大重试次数,触发recover)
        when(mockRestService.call(any(String.class), any(HttpEntity.class)))
                .thenThrow(new RuntimeException("Simulated network error again"));

        // 2. 调用SUT的实际方法,并传入真实的参数
        String apiArg = "anotherApi";
        HttpEntity<?> entityArg = new HttpEntity<>("anotherBody");
        String result = deltaHelper.process(apiArg, entityArg);

        // 3. 验证结果和交互
        // 验证mockRestService的call方法被调用了2次 (maxAttemptsExpression = "2")
        verify(mockRestService, times(2)).call(apiArg, entityArg);
        // 验证最终结果是recover方法的返回值
        assertThat(result).isEqualTo("recover_success");
        // 验证mockMyStorageService的save方法被调用了1次 (在recover方法中)
        verify(mockMyStorageService, times(1)).save(apiArg);
    }

    @Configuration
    @EnableRetry // 启用Spring Retry功能
    // @EnableAspectJAutoProxy(proxyTargetClass=true) // 如果需要AspectJ代理,但对于Spring Retry通常是CGLIB代理
    @Import({DeltaHelper.
登录后复制

以上就是正确地单元测试Spring Retry组件:避免常见陷阱与最佳实践的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号