【发布时间】:2019-02-16 06:35:56
【问题描述】:
我正在从 openInputStream 读取数据块并想模拟它的读取方法。
final byte[] testFileData = { 0x74, 0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x0d, 0x0a};
CloudBlob cloudBlob = this.getCloudBlobContainer().getBlobReferenceFromServer(blobName);
ByteArrayOutputStream blobStream = new ByteArrayOutputStream();
try (final InputStream inputStream = cloudBlob.openInputStream()) {
//Read 4MB chunks of data
byte[] bufferToRead = new byte[4 * 1024 *1024];
int bytesRead = inputStream.read(bufferToRead );
while (bytesRead > 0) {
//Add only the total number of bytes read to Bytearrayoutputstream
blobStream.write(bufferToRead, 0, bytesRead);
bytesRead = inputStream.read(bufferToRead);
}
} `
我模拟了 InputStream,但在模拟它的 read 方法时发现困难,因为它接受缓冲区作为引用并在读取后将字节数组复制到它。
@Mock
private BlobInputStream inputStream;
// Mock
when(cloudBlobContainer.getBlobReferenceFromServer(anyString())).thenReturn(cloudBlob);
when(cloudBlob.openInputStream()).thenReturn(inputStream);
【问题讨论】:
标签: java junit mockito azure-storage powermockito