Mocking out Amazon AWS SDK with the Betamax Recording Proxy for testing

One of the ways you can make your testing more efficient is to mock out external services.

If you are using Amazon Web Services in your application, you can use the Betamax proxy to record and playback responses so they are consistent across your tests.

Here is an example Spock specification on how to mock out the SDK’s http service so it plays nice with Betamax:

    @Betamax(tape = "mytape")
    void 'can use betamax to stub out amazon service'() {

        given:
        // set up an AWS client configuration that uses a proxy
        ClientConfiguration clientConfig = new ClientConfiguration()
        clientConfig.proxyHost = '127.0.0.1'
        clientConfig.proxyPort = 5555

        // configure your AWS client, here we use the SimpleDBClient as an example
        AmazonSimpleDBClient simpleDbClient = new AmazonSimpleDBClient(
            new BasicAWSCredentials('accessKey', 'secretKey'),
            clientConfig
        )
        
        // setup the betamax proxy on the AWS SDK's httpClient
        BetamaxHttpsSupport.configure(simpleDbClient.client.httpClient)

        when:
        // now, when a request is made, the test will first check on the betamax tape to see if it has been previously recorded.
        SelectResult result = simpleDbClient.select(new SelectRequest("select * from MYRESOURCELIST limit 20", true))
        
        then:
        result.size() == 20
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s