Question: How do you mock DynamoDB in a test?

Answered by Rafal Wilinski
Last updated: October 23rd, 2026
Answer
Mock the SDK call used by your application, supply a controlled response, and assert the request as well as the application's result. A mock checks application behavior; it does not prove that a key schema or DynamoDB expression is valid.
With Python, botocore's Stubber can validate the expected request without sending it to AWS:
import boto3 from botocore.stub import Stubber client = boto3.client( "dynamodb", region_name="us-east-1", aws_access_key_id="testing", aws_secret_access_key="testing" ) def load_order(ddb, order_id): return ddb.get_item( TableName="Orders", Key={"pk": {"S": order_id}} ).get("Item") with Stubber(client) as stub: stub.add_response( "get_item", {"Item": {"pk": {"S": "ORDER#42"}}}, {"TableName": "Orders", "Key": {"pk": {"S": "ORDER#42"}}} ) assert load_order(client, "ORDER#42") == {"pk": {"S": "ORDER#42"}} stub.assert_no_pending_responses()
The credentials are dummy test values and the stub prevents a network request. Add separate cases for missing items, pagination, conditional failures and retryable errors where your code handles them. Reset mocks between tests so call history and responses do not leak.
The same boundary applies to JavaScript v3 command clients and Java or .NET interfaces. Use DynamoDB Local for request and expression integration tests, then validate IAM, throttling and service-specific behavior against AWS. Local behavior is not identical to production.
See boto3 examples for the underlying calls.
Source: botocore Stubber.
Other Common DynamoDB FAQ (with Answers)
- Is DynamoDB OLTP or OLAP?
- Can Power BI connect to DynamoDB?
- Does DynamoDB have read replicas?
- Is DynamoDB a relational database?
- How to import data from S3 to DynamoDB?
- How to count rows in DynamoDB?
- How do you store JSON on DynamoDB?
- Can I use a UUID as a DynamoDB partition key?
- What are the key differences between DynamoDB and Elasticsearch?
- Are DynamoDB table names unique?
- Does DynamoDB charge for storage?
- Can colons and special characters be used in DynamoDB attributes?
- Can DynamoDB store documents?
- Is DynamoDB ACID compliant?
- Can you force-delete a DynamoDB table?
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
DynamoDB Tools
- SSH GUI Client
- Chat With Cloud
- MongoDB GUI Client
- AWS Cost Monitoring
- DynamoDB Query Builder
- DynamoDB Code Examples
- DynamoDB Node.js Queries
- DynamoDB JSON Converter
- DynamoDB UUID Generator
- DynamoDB Local Admin GUI
- DynamoDB Pricing Calculator
- DynamoDB Single-Table Design
- Best DynamoDB GUI Clients
- Dynobase vs DynoTable