Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Daniel Quackenbush
Daniel Quackenbush

Posted on

     

Mocking the AWS SDK With Go

Mocking a client library is a common technique when building test-driven development. In golang, this can be done by creating structs that implement interfaces and then override the methods you are trying to mock. This example of mocking can be done with any method, but for this post, I will use AWS Organizations to demonstrate.

Implementation Code

First, we want to create a struct that will be used as the methods' instance. As you see below, we are going to implement the interface of the SDK provided organizations API:

typeOrganizationsstruct{Clientorganizationsiface.OrganizationsAPI}
Enter fullscreen modeExit fullscreen mode

Then once we have defined that, we will have to instantiate the method we want to implement, which for the main implementation will be a pass-through for the method used by the client.

func(s*Organizations)ListAccounts(in*organizations.ListAccountsInput)(*organizations.ListAccountsOutput,error){result,err:=s.Client.ListAccounts(in)returnresult,err}
Enter fullscreen modeExit fullscreen mode

Finally, we will want to create a method we will test. We must parameterize the struct to help our test define which method to use.

funcWhatAreMyAccounts(client*Organizations)(*organizations.ListAccountsOutput,error){returnclient.ListAccounts(&organizations.ListAccountsInput{MaxResults:aws.Int64(5),NextToken:nil,},)}
Enter fullscreen modeExit fullscreen mode

Test Code

For testing, we will create our Mock struct, followed by our method override, and then wrap that within a given test. The MockOrganization struct will implement an interface, which we can later utilize as the organization's client.

typeMockedOrganizationsstruct{organizationsiface.OrganizationsAPI}
Enter fullscreen modeExit fullscreen mode

For the test, we want to guarantee a response. Therefore, we will define a separate implementation of the organization interface, of which we will return a constant value for the test suite.

func(m*MockedOrganizations)ListAccounts(in*organizations.ListAccountsInput)(*organizations.ListAccountsOutput,error){return&organizations.ListAccountsOutput{Accounts:[]*organizations.Account{{Arn:aws.String(""),Email:aws.String("test1@example.com"),Id:aws.String("234567890"),Name:aws.String("test-1"),},{Arn:aws.String(""),Email:aws.String("test2@example.com"),Id:aws.String("123456789"),Name:aws.String("test-2"),},},},nil}
Enter fullscreen modeExit fullscreen mode

On the test code, by using our mocked interface, we can create an Organizations object. The application's function then calls our mocked response to yield the result to test.

funcTestListAccounts(t*testing.T){test:=Organizations{Client:&MockedOrganizations{},}resp,err:=WhatAreMyAccounts(&test)assert.Equal(t,len(resp.Accounts),2)assert.NoError(t,err)}
Enter fullscreen modeExit fullscreen mode

Results

➜  main gotest-v=== RUN   TestListAccounts--- PASS: TestListAccounts(0.00s)PASSok      main/cmd/main   0.117s
Enter fullscreen modeExit fullscreen mode

To see the full code, check out the below gists:

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
grahamcox82 profile image
Graham Cox
  • Joined

This is great, and a helpful introduction to testing the AWS SDK in my code.

However, I can't work out how to test the error cases? For example, how would I make my mock client return anAWSOrganizationsNotInUseException? As best I can tell this isn't a constructable type so I can't construct and return it the way I would withListAccountsOutput.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Work
    Senior Solutions Architect
  • Joined

More fromDaniel Quackenbush

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp