Alternative for setting AWS environment variables using mockito

Question:

I have a method in a class, where internally calls AmazonS3Client.java in which AwsCredetialsProvider.getCredentials() is been called. For that I need to set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in my test class. Setting those values in Environment Variables are not allowed in my project.

I have tried to Mock AWSCredentialsProvider, and AWSCredentials and used stubbing to return credentials. But it is throwing the following error:

Even I have tried setting those values using MockEnvironment, ConfigurationProperties too. But nothing happened.

So, is there any possible way to mock these credential values?

I am using MockitoJRunner. So, I cannot change it to Powermockito only for one method.

Edited:
I want to fetch the files from S3 by calling listObjects(). This is where I am facing the issue as it needs AWS credentials to fetch.

All the method callings are project specified where internally calling predefined classes and methods.

Answer:

Consider mocking AmazonS3Client class instead of something it uses internally. If some day AWS library will get an update that changes AmazonS3Client internals then you might have your tests broken.


And then set the object to be returned

Leave a Reply