Junior — Middle
Explain the differences between a mock object and a stub in software testing.
sobes.tech AI
Answer from AI
In software testing:
-
Stub — is a simple object that provides predefined responses to calls, usually without logic. It is used to isolate the tested code from external dependencies.
-
Mock object — is a more complex object that not only mimics behavior but also verifies how it was called (for example, with what parameters and how many times). Mocks are used to check interactions.
Example:
- A stub returns a fixed value to test logic that depends on this value.
- A mock verifies that a method was called with expected parameters.
Thus, a stub is responsible for data substitution, and a mock for interaction verification.