CS FundamentalsVery hardGATE CS · TCS NQT Prime · Product Company SDE

What is the difference between a mock and a stub in unit testing terminology?

  1. A. A stub always throws an exception; a mock never does
  2. B. They are exactly the same thing with different names
  3. C. A stub simply returns canned responses to calls it receives; a mock additionally lets the test verify that specific calls were actually made to it, with specific arguments
  4. D. A mock can only be used for databases; a stub only for network calls

Correct answer: C. A stub simply returns canned responses to calls it receives; a mock additionally lets the test verify that specific calls were actually made to it, with specific arguments

Stubs are concerned with providing indirect input to the system under test (canned return values); mocks additionally support behaviour verification, letting the test assert that particular interactions happened.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic