I'm working with Java and I'm using the AWS SDK for interact with S3. I've the following method and I want to unit test it
private final S3Client s3Client;
...
...
public byte[] download(String key) throws IOException {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket("myBucket")
.key(key)
.build();
return s3Client.getObject(getObjectRequest).readAllBytes();
}
For this purpose I'm using JUnit 5 and Mockito. The problem is that I don't know how to mock the result of
s3Client.getObject(getObjectRequest)
because the return type
ResponseInputStream<GetObjectResponse>
is a final class.
Any idea or suggestions? Thank you
In case anyone is still looking for a different solution this is how I did it:
This is the code that needs to be mocked:
InputStream objectStream =
this.s3Client.getObject(
GetObjectRequest.builder().bucket(bucket).key(key).build(),
ResponseTransformer.toInputStream());
This is how to mock it:
S3Client s3Client = Mockito.mock(S3Client.class);
String bucket = "bucket";
String key = "key";
InputStream objectStream = getFakeInputStream();
when(s3Client.getObject(
Mockito.any(GetObjectRequest.class),
ArgumentMatchers
.<ResponseTransformer<GetObjectResponse, ResponseInputStream<GetObjectResponse>>>
any()))
.then(
invocation -> {
GetObjectRequest getObjectRequest = invocation.getArgument(0);
assertEquals(bucket, getObjectRequest.bucket());
assertEquals(key, getObjectRequest.key());
return new ResponseInputStream<>(
GetObjectResponse.builder().build(), AbortableInputStream.create(objectStream));
});
The problem is solved. In a maven project you can add a file named "org.mockito.plugins.MockMaker" in the folder "src/test/resources/mockito-extensions".
Inside the file, add "mock-maker-inline" without quotes.
From now Mockito will be able to mock final classes also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With