Skip to content

Provides mocked versions of the Java AWS SDK clients

License

Notifications You must be signed in to change notification settings

Zipabout/mock-aws-java-sdk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Test codecov License: Unlicense

mock-aws-java-sdk

Provides mocked versions of the Java AWS SDK clients, for use in unit tests. Supporting:

  • V1 Java SDK
  • V2 Java SDK

Requirements

  • java 8 and above
  • AWS SDKs are not bundled, so you can pick and choose which ones you want

Install

Follow the instructions on Jitpack.

QuickStart

class GameService(private val sns: SnsClient, private val eventsTopicArn: String) {

    private val gamesDao = mutableMapOf<UUID, String>()

    operator fun get(id: UUID) = gamesDao[id]

    fun createGame(name: String): UUID {
        val id = UUID.randomUUID()
        gamesDao[id] = name

        sns.publish {
            it.topicArn(eventsTopicArn)
            it.message(name)
        }

        return id
    }
}

// inject a real client for real use
fun main(args: Array<String>) {
    val eventsTopicArn = args.first()
    val sns = SnsClient.create()
    val service = GameService(sns, eventsTopicArn)
    // configure API and start server...
}

class GameServiceTest {

    private val backend = MockSnsBackend()
    private val topic = backend.createTopic("game-events")
    
    // inject a mock client for tests
    private val testObj = GameService(
        sns = MockSnsV2(backend),
        eventsTopicArn = topic.arn
    )

    @Test
    fun `create game`() {
        val id = testObj.createGame("Mass Effect 3")
        Assertions.assertThat(testObj[id]).isEqualTo("Mass Effect 3")
        Assertions.assertThat(topic.messages().map { it.message }).containsExactly("Mass Effect 3")
    }
}

How it Works

Each mocked SDK implements the same interface as the standard SDKs. While the standard ones will delegate the calls to the AWS REST API, these clients will delegate them to a mock backend.

Instead of allowing your classes under test to initialize their own SDK, you should update them to allow the SDK to be injected; your main runner will inject real SDKs, and your tests will inject the mocks.

Note: Since the AWS resources created in the mock backends are in-memory, they will not persist.

Note: If you use the v1 SDK, make sure you inject the v1 mock. The same applies for the v2 SDK.

Supported Services

Service SDKs Support
S3 MockS3V1
MockS3V2
✔️ Core Functionality
❌ Object Metadata
❌ Permissions
SQS MockSqsV1
MockSqsV2
✔️ Core Functionality
❌ Message Attributes
Dynamo DB MockDynamoDbV1
MockDynamoDbV2
✔️ Core Functionality
✔️ Mapper
✔️ Conditions
✴️ (Partial) Filter Expressions
❌ Conditional Operations
❌ Projections
SSM MockSsmV1
MockSsmV2
✔️ Parameter Store
Secrets Manager MockSecretsManagerV1
MockSecretsManagerV2
✔️ Core Functionality
❌ Secret Rotation
SNS MockSnsV1
MockSnsV2
✔️ Create/Delete Topic
✔️ Publish to Topic
❌ Subscriptions
Cloudformation MockCloudformationV1
MockCloudformationV2
✔️ Exports Only

Samples

There are some Sample Snippets available to help get you started.

About

Provides mocked versions of the Java AWS SDK clients

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%