我正在为我的CacheService
班级撰写单元测验:
public class CacheService {
private final HashOperations<String, String, String> redisHashOps;
private final ValueOperations<String, String> valueOps;
private final SetOperations<String, String> setOps;
public CacheService(StringRedisTemplate redisTemplate) {
this.redisHashOps = redisTemplate.opsForHash();
this.valueOps = redisTemplate.opsForValue();
this.setOps = redisTemplate.opsForSet();
}
单元测验:
public class CacheServiceTest {
private CacheService cacheService;
private HashOperations<String, String, String> redisHashOps;
private ValueOperations<String, String> redisValueOps;
private SetOperations<String, String> redisSetOps;
@BeforeEach
public void setUp() {
final StringRedisTemplate redisTemplate = mock(StringRedisTemplate.class);
redisValueOps = mock(ValueOperations.class);
redisHashOps = mock(HashOperations.class);
redisSetOps = mock(SetOperations.class);
when(redisTemplate.opsForValue()).thenReturn(redisValueOps);
when(redisTemplate.opsForHash()).thenReturn(redisHashOps);
when(redisTemplate.opsForSet()).thenReturn(redisSetOps);
redisValueOps
并且redisSetOps
没有问题,但是对于redisHashOps
,IDE 提示:
如果我构建项目将看到以下错误:
java: no suitable method found for thenReturn(org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.String,java.lang.String>)
method org.mockito.stubbing.OngoingStubbing.thenReturn(org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.Object,java.lang.Object>) is not applicable
(argument mismatch; org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.String,java.lang.String> cannot be converted to org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.Object,java.lang.Object>)
method org.mockito.stubbing.OngoingStubbing.thenReturn(org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.Object,java.lang.Object>,org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.Object,java.lang.Object>...) is not applicable
(argument mismatch; org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.String,java.lang.String> cannot be converted to org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.Object,java.lang.Object>)
查看RedisTemplate的原始码发现了opsForHash和opsForValue的区别:
public <HK, HV> HashOperations<K, HK, HV> opsForHash() ...
public ValueOperations<K, V> opsForValue() ...
如何修改单元测验以使其作业?
uj5u.com热心网友回复:
您可以doReturn
改用以下方法重写您的模拟期望:
doReturn(redisHashOps).when(redisTemplate).opsForHash();
uj5u.com热心网友回复:
你发现它是正确的:
在第一种(也是最后一种)情况下<K, V>
是指RedisTemplate
泛型自变量(在<String, String>
此处分配)。
在 的情况下<HK, HV> HashOperations<K, HK, HV>
,K
是“再次安全”,但是<HK, HV>
是“方法泛型自变量”,编译器无法在此处推断。
解决方案:
- 正如 armandino 正确建议的那样:
我们让编译器免于猜测。doReturn(redisHashOps).when(redisTemplate).opsForHash();
- 或通过“通用自变量”:
when(redisTemplate.<String,String>opsForHash()).thenReturn(redisHashOps);
- 或“原始”/残酷的方法:
private HashOperations<String, Object, Object> redisHashOps; ... when(redisTemplate.opsForHash()).thenReturn(redisHashOps);
0 评论