Shailendra
Shailendra

Reputation: 141

How to mock S3 in order to test Java code in Junit

I have taken reference form here in order to implement S3 mocking for integration testing

My confusion and the part that i did not get is that how can i use docker here ? Do i have to install something ?

I have just added below code in my maven

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-datapipeline</artifactId>
            <version>1.11.295</version>
        </dependency>

And have written below class

package com.amazonaws.lambda.demo;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Builder;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import io.findify.s3mock.S3Mock;

public class S3Mock {
     
    S3Mock api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build();
    api.start();

    AmazonS3Client client = new AmazonS3Client(new AnonymousAWSCredentials());
    // use local API mock, not the AWS one
    client.setEndpoint("http://127.0.0.1:8001");
    client.createBucket("testbucket");
    client.putObject("testbucket", "file/name", "contents");
}

But i am not able to compile my class itself . Can some body help me understand what else i have to do in order to make this work .

Upvotes: 2

Views: 10151

Answers (1)

piy26
piy26

Reputation: 1592

I believe you have missed the dependency for S3Mock which comes with below maven:

<dependency>
    <groupId>io.findify</groupId>
    <artifactId>s3mock_2.12</artifactId>
    <version>0.2.5</version>
    <scope>test</scope>
</dependency>

Also, I will suggest you rename your class from S3Mock to something like AmazonS3Mock just to avoid namespace confusions

Upvotes: 2

Related Questions