如何模拟InputStream和ByteString

时间:2015-06-18 05:45:25

标签: java jmockit

我有这样的方法

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来模拟输入流,无法找到如何模拟ByteStringbyte[]

1 个答案:

答案 0 :(得分:1)

我会这样做:

Mockito
    .when(ByteString.copyFrom(Mockito.<byte[]>any()))
    .thenReturn(ByteString.copyFromUtf8("byteString was mocked"));