Search code examples
javajunitamazon-s3integration-testing

How to mock Amazon S3 in an integration test


I'm trying to get an "walking skeleton" of my app up that will use S3 for persistence. I would like to use a fake S3 service so each developer's desktop can read/write at will.

I thought mocks3 would be perfect, as I could get a jetty server up in my jUnit tests. The problem is that mocks3 doesn't allow any writes. Not even to set it up as far as I can tell.

So how do others do this?


Solution

  • There is also an Findify s3mock tool written exactly for this purpose. It mocks the essential parts of AWS S3 API on top of local filesystem:

    import io.findify.s3mock.S3Mock
    
    S3Mock api = S3Mock.create(8001, "/tmp/s3");
    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");
    

    It's also easily embeddable and configuration-less.