DVA-C02 Question 274
Single answerYou are developing an AWS Lambda function that interacts with an Amazon DynamoDB table to store user information. To ensure code quality, you want to automate unit tests for your function without making actual calls to DynamoDB. Which approach should you take?
- A
Write unit tests that directly interact with the DynamoDB table in your AWS account to validate the function's behavior.
- B
Use a mocking library like AWS SDK Mock to simulate DynamoDB API calls in your unit tests.
- C
Configure an Amazon DynamoDB local instance and use it for all unit tests.
- D
Rely on manual testing in the AWS Management Console instead of writing automated tests.
Show answer and explanation
Correct answer: B
Explanation
The correct answer is to use a mocking library like AWS SDK Mock to simulate DynamoDB API calls in your unit tests. Unit testing focuses on testing code in isolation, without dependencies on external resources like DynamoDB. Mocking ensures that tests are fast and reliable while verifying the logic of your function.
- A. Incorrect.
Directly interacting with the DynamoDB table in your AWS account for unit tests is not recommended because it introduces external dependencies, making the test less isolated and slower.
- B. Correct.
Using a mocking library like AWS SDK Mock is the correct approach because it allows you to simulate DynamoDB API calls, ensuring your unit tests are fast, isolated, and do not depend on actual AWS resources.
- C. Incorrect.
Using a DynamoDB local instance is more suited for integration testing rather than unit testing, as it still involves external configuration and dependencies.
- D. Incorrect.
Relying on manual testing is not recommended because it is time-consuming, error-prone, and goes against the principles of automated software testing.