What is the difference between Mock or Stub?

Category: Microservices ArchitectureWhat is the difference between Mock or Stub?
Editor">Editor Staff asked 2 years ago

What is the difference between Mock or Stub?

1 Answers
Editor">Editor Staff answered 2 years ago

Stub

  • A dummy object that helps in running the test.
  • Provides fixed behavior under certain conditions which can be hard-coded.
  • Any other behavior of the stub is never tested.

For example, for an empty stack, you can create a stub that just returns true for empty() method. So, this does not care whether there is an element in the stack or not.
Mock

  • A dummy object in which certain properties are set initially.
  • The behavior of this object depends on the set properties.
  • The object’s behavior can also be tested.

For example, for a Customer object, you can mock it by setting name and age. You can set age as 12 and then test for isAdult() method that will return true for age greater than 18. So, your Mock Customer object works for the specified condition.