我有课
public class MyMainClass{
public void mainLogic(){
handleEvictedKeys( key, value, removalCause)
}
private void handleEvictedKeys(final Object key, final Object value, final RemovalCause removalCause) {
if (removalCause.equals(RemovalCause.SIZE)) {
remote.setOrReplaceValueForKey((String) key, (Map<String, String>) value);
} else {
log.warn("Key {} and Value {} was evicted from cache for theme {} for the reason {}", key, value,
cacheInformation.getThemeName(), removalCause);
}
}
GenericCacheBuilder genericCacheBuilder = CacheSystemToCacheManagerProvider
.getCacheBuilder(cacheInformation.getRemoteCachePreference());
return (RemoteCacheManager) genericCacheBuilder.buildCacheManager(cacheInformation);
}
}
我的远程提供商
public class CacheSystemToCacheManagerProvider {
private static final ImmutableMap<AllowedCacheSystem, GenericCacheBuilder<?>> CACHE_SYSTEM_TO_CACHE_BUILDER_MAP =
ImmutableMap.<AllowedCacheSystem, GenericCacheBuilder<?>>builder()
.put(AllowedLocalCacheSystem.COMMON_CAFFEINE, new CommonCaffeineCacheBuilder())
.put(AllowedRemoteCacheSystem.COMMON_REDIS, new CommonRedisCacheBuilder())
.build();
public static GenericCacheBuilder getCacheBuilder(AllowedCacheSystem cacheSystem) {
return CACHE_SYSTEM_TO_CACHE_BUILDER_MAP.get(cacheSystem);
}
}
当我尝试为MyMainClass编写单元测试时,
public class TestMyMainClass(){
//all dependencies
@InjectMocks
public MyMainClass mtestClass;
public void mymainLogic(){
cacheSystemToCacheManagerProviderStatic = Mockito.mockStatic(CacheSystemToCacheManagerProvider.class);
cacheSystemToCacheManagerProviderStatic.when(
CacheSystemToCacheManagerProvider::getCacheBuilder)
.thenReturn(getCacheBuilderMock);
mtestClass.handleEvictedKeys(a,b,c)
}
}
我试图模拟调用CacheSystemToCacheManagerProvider.getCacheBuilder(),但这是在进行实际调用,并试图创建new CommonCaffeineCacheBuilder()
的示例,这会带来很多依赖关系并破坏UT。
如何正确模拟此静态调用
1条答案
按热度按时间00jrzges1#
对
new CommonCaffeineCacheBuilder()
的调用不会在执行getCacheBuilder
时发生。它发生在该点之前,在加载CacheSystemToCacheManagerProvider
类时立即执行的静态初始化器中。在有机会嘲笑它之前,这必然会发生。更好的设计将使
CacheSystemToCacheManagerProvider
在MyMainClass
中 * 可替换 *,使其更容易被模仿:关键是使
CacheSystemToCacheManagerProvider
成为一个 * 接口 *:对于使用真实的缓存提供程序的 * 实现 *:
这确保了在测试中不会创建
CommonCaffeineCacheBuilder
和CommonRedisCacheBuilder
示例。