9

So I want to mock AWS SDK 2.x (only S3 service), so that test upload and download files to a bucket without a real AWS. I'm looking for a Java implementations.

I found a lot what implements (Adobe S3Mock ...etc), but all of them uses AWS SDK 1.x.

madhead
  • 31,729
  • 16
  • 153
  • 201
Gábor Csikós
  • 2,787
  • 7
  • 31
  • 57
  • Well, you can wrap the library by calling it a connector or a service and do unit-test by mocking your own class. This will only work for simple unit-tests. – Andre Jan 17 '22 at 09:48

1 Answers1

6

You shouldn't re-implement the S3 API yourself. Instead, use one of the existing tools like localstack, MinIO, OpenStack Swift and others. They all provide S3-compatible API (though some dif ferences can be). The best one for tests is localstack, IMHO.It is available as a Docker image, so you can use it locally or in your CI pipelines.

Not to the Java part of your question. If you are a lucky user of JUnit 5 I would recommend you to use aws-junit5, a set of JUnit 5 extensions for AWS. And, yes, I am it's author. These extensions can be used to inject clients for AWS services provided by tools like localstack (actually, any AWS-compatible API is supported, including real AWS itself). Both AWS Java SDK v 2.x and v 1.x are supported. You can use aws-junit5 to inject clients for S3, DynamoDB, Kinesis, SES, SNS and SQS.

Read more in the user guide, it even has a section about CI configuration with GitHub.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • 2
    How can I mock S3client without docker. I am writing unit test cases for S3. Can you post some example of mocking S3Client (aws sdk v2) in localStack or in MinIO. – Krishna Kumar Singh Oct 22 '20 at 14:12
  • just wants to test it like this: S3Client s3Client = S3Client.builder() .region(region) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))) .endpointOverride(URI.create("http://localhost:9192")) .httpClient(UrlConnectionHttpClient.builder().buildWithDefaults( AttributeMap.builder().put(TRUST_ALL_CERTIFICATES, java.lang.Boolean.TRUE).build() )) .build(); – Krishna Kumar Singh Oct 22 '20 at 14:17