How to mock InputStream and ByteString

1.9k views Asked by At

I have a method like this

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);
    }
} 

Is there any way to mock these objects?

Currently i am using @Mocked final Process mockProcess to mock the input stream couldn't able to find out how to mock ByteString and byte[].

1

There are 1 answers

2
Rufi On BEST ANSWER

I would go with this:

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