
在单元测试spring retry功能时,开发者常遇到依赖注入为空或mockito用法不当的问题。本文将深入探讨如何正确配置spring测试环境,特别是如何有效模拟依赖,避免在测试系统核心逻辑时将真实对象误作模拟对象,以及如何规范使用`argumentmatchers.any()`,确保`@autowired`的bean能够正确注入,并使spring retry机制在测试中按预期工作。
Spring Retry通过AOP(面向切面编程)实现,这意味着它需要在Spring应用上下文中运行才能生效。因此,简单的JUnit测试无法激活其重试机制。我们需要借助Spring的测试工具,如SpringRunner(或Spring Boot的@SpringBootTest),并配置一个最小化的Spring应用上下文。
当使用@RunWith(SpringRunner.class)和@ContextConfiguration时,Spring会加载指定的配置类,并管理Bean的生命周期和依赖注入。然而,在测试中,我们往往需要隔离被测单元(System Under Test, SUT)的外部依赖,以便控制其行为并专注于SUT本身的逻辑。
在测试包含@Retryable注解的Spring组件时,开发者常会遇到以下两个主要问题:
问题描述: 开发者有时会尝试直接对被测类(SUT)的实例进行when()调用设置,例如 when(deltaHelper.restService.call(...)).thenThrow(...)。如果deltaHelper是真实的SUT实例,其内部的restService也是一个真实对象,那么when()方法将无法对其进行行为模拟,因为when()只能用于Mock对象。这会导致deltaHelper.restService`在测试执行时表现出真实行为,而不是我们期望的模拟行为,甚至可能因为未正确注入而导致空指针。
解决方案: 正确的做法是模拟SUT的依赖,而不是SUT本身。SUT应该是一个真实的Spring Bean,其依赖则应被替换为Mock对象。这样,我们可以在Mock依赖上设置期望的行为(例如抛出异常以触发重试),从而测试SUT在不同依赖行为下的响应。
问题描述:ArgumentMatchers.any()(如any()、anyString()等)是Mockito提供的一种匹配器,用于在设置Mock行为(when())或验证Mock交互(verify())时匹配任何参数。然而,any()方法在被调用时会直接返回null(或对应基本类型的默认值)。如果在对SUT的实际方法调用中(即“act”阶段)使用any(),例如 deltaHelper.process(any(), any()),那么SUT接收到的参数将是null,这很可能导致空指针异常或其他非预期行为。
解决方案: 在调用SUT的实际方法时,必须传入真实的、有意义的参数值。any()仅应用于Mock对象的行为设置或验证。
结合上述解决方案,以下是针对DeltaHelper类的优化测试示例。我们将确保DeltaHelper是一个真实的Spring Bean,但其内部依赖MyRestService和MyStorageService将被替换为Mock对象。
首先,我们假设DeltaHelper、MyRestService和MyStorageService等业务组件已按常规Spring方式定义:
// DeltaHelper.java
@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 process for API: " + api); // 方便观察重试
return restService.call(api, entity);
}
@Recover
public String recover(Exception e, String api, HttpEntity<?> entity) {
System.out.println("Recovering from exception for API: " + api + " - " + e.getMessage());
myStorageService.save(api);
return "recover";
}
}
// MyRestService.java
@Service
public class MyRestService extends org.springframework.web.client.RestTemplate {
// 假设call方法存在并被DeltaHelper调用
public String call(String api, HttpEntity<?> entity) {
// 实际的REST调用逻辑
throw new UnsupportedOperationException("Not implemented for real usage in test");
}
}
// MyStorageService.java
@Service
public class MyStorageService {
@Autowired
MyRepo myRepo;
@Async
public MyEntity save(String api) {
System.out.println("Saving API: " + api + " to storage.");
return myRepo.save(new MyEntity(api, System.currentTimeMillis()));
}
}
// MyRepo.java (接口或抽象类)
public interface MyRepo {
MyEntity save(MyEntity entity);
}
// MyEntity.java
public class MyEntity {
private String api;
private long timestamp;
public MyEntity(String api, long timestamp) {
this.api = api;
this.timestamp = timestamp;
}
// getters, setters
}接下来是修正后的测试类:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpEntity;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@RunWith(SpringRunner.class)
@ContextConfiguration
public class DeltaHelperTest {
@Autowired
private DeltaHelper deltaHelper; // SUT,由Spring管理并注入Mock依赖
@Autowired
private MyRestService mockRestService; // 注入Mock对象,用于设置行为和验证
@Autowired
private MyStorageService mockMyStorageService; // 注入Mock对象
@Autowired
private MyRepo mockMyRepo; // 注入Mock对象
@Before
public void setUp() {
// 设置重试次数,确保@Retryable的maxAttemptsExpression能正确解析
System.setProperty("delta.process.retries", "2");
// 重置所有Mock,确保每个测试方法都是干净的环境
Mockito.reset(mockRestService, mockMyStorageService, mockMyRepo);
}
@After
public void validate() {
// 验证Mock的使用,确保没有未验证的交互
validateMockitoUsage();
}
@Test
public void retriesAfterOneFailAndThenPass() throws Exception {
String testApi = "test-api-path";
HttpEntity<?> testEntity = new HttpEntity<>("test-body");
// 模拟restService的第一次调用抛出异常,第二次成功
when(mockRestService.call(eq(testApi), eq(testEntity)))
.thenThrow(new RuntimeException("Simulated first call failure")) // 第一次失败
.thenReturn("success-response"); // 第二次成功
// 调用SUT的方法,传入真实的参数
String result = deltaHelper.process(testApi, testEntity);
// 验证restService的call方法被调用了两次(一次失败,一次成功)
verify(mockRestService, times(2)).call(eq(testApi), eq(testEntity));
// 验证重试成功后返回的是第二次调用的结果
assert "success-response".equals(result);
// 验证recover方法没有被调用,因为重试成功了
verify(mockMyStorageService, never()).save(anyString());
}
@Test
public void retriesFailAndThenRecover() throws Exception {
String testApi = "fail-api-path";
HttpEntity<?> testEntity = new HttpEntity<>("fail-body");
// 模拟restService的两次调用都抛出异常,触发recover
when(mockRestService.call(eq(testApi), eq(testEntity)))
.thenThrow(new RuntimeException("Simulated first call failure"))
.thenThrow(new RuntimeException("Simulated second call failure")); // 第二次也失败
// 调用SUT的方法,传入真实的参数
String result = deltaHelper.process(testApi, testEntity);
// 验证restService的call方法被调用了两次(达到maxAttemptsExpression设定的次数)
verify(mockRestService, times(2)).call(eq(testApi), eq(testEntity));
// 验证recover方法被调用了,因为重试失败
verify(mockMyStorageService, times(1)).save(eq(testApi));
// 验证返回的是recover方法的结果
assert "recover".equals(result);
}
@Configuration
@EnableRetry // 启用Spring Retry
@EnableAspectJAutoProxy(proxyTargetClass = true) // 启用AspectJ代理,确保@Retryable生效
public static class Application {
// DeltaHelper作为SUT,让Spring正常创建和注入
@Bean
public DeltaHelper deltaHelper() {
return new DeltaHelper();
}
// 提供MyRestService的Mock Bean
@Bean
public MyRestService restService() {
return mock(MyRestService.class);
}
// 提供MyStorageService的Mock Bean
@Bean
public MyStorageService myStorageService() {
return mock(MyStorageService.class);
}
// 提供MyRepo的Mock Bean
@Bean
public MyRepo myRepository() {
return mock(MyRepo.class);
}
}
}代码解释与改进点:
SUT作为真实Bean,依赖作为Mock Bean:
正确使用when()和verify():
@EnableAspectJAutoProxy(proxyTargetClass = true):
System.setProperty("delta.process.retries", "2"):
Mockito.reset(...):
通过遵循这些原则和实践,您可以更有效地对包含Spring Retry功能的组件进行单元测试,确保代码的健壮性和正确性。
以上就是正确测试Spring Retry组件:避免空指针与Mockito误用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号