如何模拟 InputStream 和 ByteString
How to mock InputStream and ByteString
我有这样的方法
private byte[] getInternalMDPayload(String metaDataDirString)
{
byte[] data = new byte[16384];
try
{
final InputStream internalMetadataInputStream = this.getClass().getClassLoader()
.getResourceAsStream(metaDataDirString);
data = ByteStreams.toByteArray(internalMetadataInputStream);
byteString = ByteString.copyFrom(data);
} catch (IOException e)
{
dl.debug("Error occurred while loading the metadata file" + metaDataDirString);
}
}
有什么方法可以模拟这些对象吗?
目前我正在使用 @Mocked final Process mockProcess
来模拟输入流无法找到如何模拟 ByteString
和 byte[]
。
我会选择这个:
Mockito
.when(ByteString.copyFrom(Mockito.<byte[]>any()))
.thenReturn(ByteString.copyFromUtf8("byteString was mocked"));
我有这样的方法
private byte[] getInternalMDPayload(String metaDataDirString)
{
byte[] data = new byte[16384];
try
{
final InputStream internalMetadataInputStream = this.getClass().getClassLoader()
.getResourceAsStream(metaDataDirString);
data = ByteStreams.toByteArray(internalMetadataInputStream);
byteString = ByteString.copyFrom(data);
} catch (IOException e)
{
dl.debug("Error occurred while loading the metadata file" + metaDataDirString);
}
}
有什么方法可以模拟这些对象吗?
目前我正在使用 @Mocked final Process mockProcess
来模拟输入流无法找到如何模拟 ByteString
和 byte[]
。
我会选择这个:
Mockito
.when(ByteString.copyFrom(Mockito.<byte[]>any()))
.thenReturn(ByteString.copyFromUtf8("byteString was mocked"));