SteelReed

Search

Search posts, questions…

TestingQuestion 1 / 9

Junior

What is the test pyramid, when should you unit-test a method, and when should you mock external dependencies?

Short answer

Test pyramid: many unit tests (fast, cheap), fewer integration tests (real DB, HTTP), very few end-to-end tests (slow). Unit-test logic with branches.

don't mock directlyE2E: very fewslow, flakyIntegration: fewerreal DB, HTTPUnit: the mostfast, cheapPayment SDK,RestTemplateWrap behind interfaceWireMock, contract test

3 points to say:

  1. Many unit, fewer integration, very few E2E.
  2. Business logic: unit tests; queries and mapping: integration tests.
  3. Don't mock what you don't own: wrap it behind an interface.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.