Bird
0
0

You want to unit test a microservice method that calls two other services: a user service and an inventory service. Which approach best ensures your unit test is reliable and fast?

hard📝 Trade-off Q15 of 15
Microservices - Testing Microservices
You want to unit test a microservice method that calls two other services: a user service and an inventory service. Which approach best ensures your unit test is reliable and fast?
ASkip testing this method because it depends on other services
BCall both real services during the test to check integration
CMock both user and inventory service calls with fixed responses
DTest only the user service call and ignore inventory service
Step-by-Step Solution
Solution:
  1. Step 1: Understand unit test isolation

    Unit tests should isolate the method by mocking external service calls to avoid flakiness and slowness.
  2. Step 2: Apply mocks to all external dependencies

    Mocking both user and inventory service calls ensures the test is reliable and fast without real network calls.
  3. Final Answer:

    Mock both user and inventory service calls with fixed responses -> Option C
  4. Quick Check:

    Mock all external calls for reliable, fast unit tests [OK]
Quick Trick: Mock all external services for isolated unit tests [OK]
Common Mistakes:
  • Calling real services in unit tests
  • Skipping tests due to dependencies
  • Partially mocking dependencies leading to flaky tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes