Tuesday, March 12, 2024

CST438 Week 2

In lab 1 on Junit you used a Mock. In your own words describe what a Mock is and why it is useful in unit testing.

Mocks are stand-ins that sit behind a standardized interface of a program. A Mock is used to return predetermined / controlled values back to the caller, often in an effort to test the caller’s eventual result when given the values supplied by the Mock. This is an extremely useful tool when implementing unit tests, as it allows certain functions, classes, etc. to be isolated from any external dependencies they might rely upon. For example, if a function normally executes a database query to pull some data, then performs an operation on that data, a Mock could be used to spoof the database access. The tested function would still believe it had executed a database query to receive the data, but in reality, it was fed some predetermined values – which allows the function to be tested in a repeatable, and deterministic manner.

No comments:

Post a Comment