新闻中心
正确地单元测试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的模拟。
Ghiblio
专业AI吉卜力风格转换平台,将生活照变身吉卜力风格照
157
查看详情
前提: 假设DeltaHelper、MyStorageService是Spring组件(例如,带有@Component或@Service注解),MyRestService是一个简单的RestTemplate扩展类。
// DeltaHelper.j*a (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.s*e(api);
return "recover_success";
}
}
// MyRestService.j*a (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.j*a (Another Dependency)
@Service
public class MyStorageService {
@Autowired
MyRepo myRepo;
@Async
public MyEntity s*e(String api) {
System.out.println("MyStorageService.s*e invoked for API: " + api);
return myRepo.s*e(new MyEntity(api, System.currentTimeMillis()));
}
}
// MyRepo.j*a (Simple interface/class for demonstration)
public interface MyRepo {
MyEntity s*e(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的s*e方法需要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()).s*e(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的s*e方法被调用了1次 (在recover方法中)
verify(mockMyStorageService, times(1)).s*e(apiArg);
}
@Configuration
@EnableRetry // 启用Spring Retry功能
// @EnableAspectJAutoProxy(proxyTargetClass=true) // 如果需要AspectJ代理,但对于Spring Retry通常是CGLIB代理
@Import({DeltaHelper.以上就是正确地单元测试Spring Retry组件:避免常见陷阱与最佳实践的详细内容,更多请关注其它相关文章!
# 三大
# 惊雷seo
# 盛唐春节宴会营销推广
# 格力品牌营销推广
# 西藏怎么做网站优化
# 龙华网站建设推广平台
# 申请网站建设需要的材料
# seo制作规则
# 搜索引擎推广网站报价表
# 淮北网站建设现状
# 塔城抖音营销推广招聘
# 面向对象
# 四种
# 你应该
# java
# 抛出
# 是一个
# 正确地
# 死锁
# 单元测试
# 重试
# red
# spring框架
# 常见问题
# springboot
# proxy
# ai
# 工具
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
PySpark中高效提取字符串右侧可变长度数字:使用regexp_extract
谷歌学术网站直达地址 谷歌学术搜索网页版一键进入
如何使用纯J*aScript判断Input元素是否在特定类容器内
sublime怎么设置启动时打开的窗口_sublime会话管理与热退出
必由学官方网站入口 必由学学生教师共用登录通道
网站内容防复制粘贴的实现策略与局限性
如何使用Rector自动化升级旧代码_通过Composer安装和配置Rector进行代码重构
文本文档写html代码怎么运行_文本文档html代码运行步骤【教程】
Odoo 16:在表单视图中基于当前记录动态修改Tree视图属性
C++如何检测键盘输入_C++ _kbhit与_getch函数非阻塞输入
React/Next.js中实现列表项的动态移动与状态管理:兼论唯一键的重要性
如何更改在 Excel 中打开超链接时的默认浏览器
ArrayList与LinkedList核心操作的Big-O复杂度分析
J*aScript中高效管理与清空动态列表:避免循环陷阱
PHP URL参数传递与500错误调试指南
理解Python模块与全局变量的作用域管理
PySpark中从现有列右侧提取可变长度字符创建新列的教程
怎样把文件彻底粉碎无法恢复_Windows下安全删除敏感数据【隐私保护】
三星ZFold5多任务卡顿_Samsung ZFold5流畅度提升
J*a如何使用AtomicInteger控制计数_J*a无锁计数器性能分析
C++如何操作注册表_Windows平台下C++读写注册表的API函数详解
Win11截图该按哪些键 Win11截屏完整流程解析【教程】
如何在CSS中使用visited与link控制链接颜色_visited link伪类配合
文心一言怎样用批量生成做多版文案_文心一言用批量生成做多版文案【批量创作】
蛙漫2台版漫画地址 Manwa2正版网页版链接
c++中的std::launder有什么实际用途_c++对象生命周期与指针优化
深入理解J*a合成构造器:何时以及为何阻止其生成
深入理解rpy2中的类型转换:优化Python对象到R矩阵的映射
Lar*el头像管理:图片缩放与旧文件删除的最佳实践
《主播少女的秘密账号迷宫》首支宣传片
Win11怎么开启高性能模式_Windows 11电源计划优化设置
Golang如何安装Swagger工具_GoSwagger文档生成环境
Python多线程中正确使用sigwait处理SIGALRM信号
汽水音乐在线解析 汽水音乐在线解析入口
Python大型XML文件高效流式解析教程
《铁拳8》黑皮辣妹新实机:元气满满的18岁少女!
BetterDiscord插件中安全更新用户简介的实践指南
python3时间如何用calendar输出?
如何创建独立于主系统的J*a运行环境_隔离式环境搭建策略
LINUX怎么设置定时任务_LINUX crontab配置教程
微博网页版怎么开启两步验证_微博网页版账号安全两步验证设置方法
J*aScript类型检查_j*ascript代码规范
PyTorch模型训练效果不佳?深入剖析常见错误与调试技巧
Lar*el DB::listen 事件中的查询执行时间单位解析
优化大型XML文件解析:基于Python流式处理的内存高效方案
Win11怎么关闭触摸屏_Windows 11禁用HID符合标准触摸屏
Bilibili动漫最新防封地址发布-Bilibili动漫2025年最稳正版入口推荐
Win11 BitLocker密码忘了怎么办 Win11找回BitLocker恢复密钥方法【解决】
抖音网页版怎么|直播|_抖音网页版开播操作指南
iCloud登录入口网页版 苹果iCloud官网登录


2025-12-12
浏览次数:次
返回列表
timestamp) {
this.api = api;
this.timestamp = timestamp;
}
// Getters and Setters
}