volume_mute

Mock Objects - Purpose and Use

publish date2026/06/19 10:51:13.299306 UTC

volume_mute

A team is implementing a component that sends emails via an external SMTP server. During unit testing, they do not want to send real emails but still need to verify that their component correctly formats and triggers email sends. What is the most appropriate approach?

Correct Answer

Use a mock object that has the same interface as the SMTP service but records calls without sending real emails

Explanation

A mock SMTP object has the same interface as the real email service but simulates its behavior - recording calls, checking parameters, and returning predetermined results without sending real emails. This allows fast, isolated unit testing of the email-sending component without requiring a live SMTP server, avoiding slow setup, side effects (real emails), and external dependencies.

Reference

Software Engineering, Ian Sommerville, 10th edition


Quizzes you can take where this question appears