What are the differences between a mock and stub?


 "Mock" and "Stub" are two commonly used terms in software testing. They are used to simulate the behavior of real objects in order to test other parts of the system.

A "Stub" is a minimal implementation of an object that returns hardcoded responses to method calls. The main purpose of a stub is to provide controlled and consistent responses to method calls in a test environment. Stubs are typically used to replace the behavior of external dependencies, such as a database or a web service, in order to isolate the system under test.

A "Mock" is a more advanced version of a stub that can verify whether a method was called with the expected parameters, and whether it was called the expected number of times. Mocks also allow you to define the behavior of a method when it's called. In addition to returning hardcoded values, mocks can be used to assert that certain methods were called and verify their behavior.

In summary, the main difference between a stub and a mock is that a stub provides a simple, predefined response to a method call, while a mock can verify the behavior of the system under test and assert that certain methods were called with the expected parameters.

You may also like

No comments: