出于测试目的,我想模拟 Cloud Storage,因为它会减慢测试速度。
有谷歌云存储模拟器吗?
谷歌有一个内存模拟器 https://github.com/googleapis/java-storage-nio可以使用(大部分核心功能都实现了)。
你需要com.google.cloud:google-cloud-nio
在您的测试类路径上(:0.25.0-alpha
现在)。然后你可以使用/注入Storage
内存中实现的接口LocalStorageHelper
测试助手服务。
用法示例:
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
@Test
public void exampleInMemoryGoogleStorageTest() {
Storage storage = LocalStorageHelper.getOptions().getService();
final String blobPath = "test/path/foo.txt";
final String testBucketName = "test-bucket";
BlobInfo blobInfo = BlobInfo.newBuilder(
BlobId.of(testBucketName, blobPath)
).build();
storage.create(blobInfo, "randomContent".getBytes(StandardCharsets.UTF_8));
Iterable<Blob> allBlobsIter = storage.list(testBucketName).getValues();
// expect to find the blob we saved when iterating over bucket blobs
assertTrue(
StreamSupport.stream(allBlobsIter.spliterator(), false)
.map(BlobInfo::getName)
.anyMatch(blobPath::equals)
);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)