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:
com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain
at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:131)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3648)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3607)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:600)
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.
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.
AmazonS3Client amazonClient = Mockito.mock(AmazonS3Client.class);
And then set the object to be returned
Mockito.doReturn(objListing).when(amazonClient).listObjects(bucketName);
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