What is a stub in unit testing?

Updated May 6, 2026

Short answer

A stub provides predefined responses to function calls.

Deep explanation

Stubs replace real implementations with controlled outputs. Unlike mocks, they don't verify interactions but only provide data.

Real-world example

Stubbing database responses in service tests.

Common mistakes

  • Using stubs when behavior verification is needed.

Follow-up questions

  • Stub vs mock difference?
  • When to use stubs?

More Unit Testing interview questions

View all →