vendredi 20 mars 2015

I want to mock a proprietary class that extends InputStream, mock read, verify close

I want to use Mockito to mock AmazonS3 and test opening a stream from it and then verifying that the stream is closed after my code has read from it. I'd also like to get the bytes read from the stream. Something like this:



AmazonS3 client = mock(AmazonS3.class);
when(tm.getAmazonS3Client()).thenReturn(client);
S3Object response = mock(S3Object.class);
when(client.getObject(any(GetObjectRequest.class))).thenReturn(response);
S3ObjectInputStream stream = mock(S3ObjectInputStream.class);
when(response.getObjectContent()).thenReturn(stream);

somehow mock the read method

MyObject me = new MyObject(client);
byte[] bra me.getBytes(File f, offset, length);
assertEquals(length, bra.length);
verify(stream).close();

Aucun commentaire:

Enregistrer un commentaire