---
title: "The Mock Objects elevator speech"
url: "https://osherove.com/blog/2005/6/30/the-mock-objects-elevator-speech.md"
canonical: "https://osherove.com/blog/2005/6/30/the-mock-objects-elevator-speech.html"
published: "2005-06-30T18:33:00"
updated: "2005-06-30T18:33:00"
tags: ["Agile Related", "Testing Guidelines"]
author: "Roy Osherove"
description: "A friend asked me by email the other day :\"So why do I need mock objects?\" I came up with this in roughly 3 minutes so it's a bit rough, but it'll do to drive the point home, I hope. Note, that this does not really…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# The Mock Objects elevator speech

By Roy Osherove June 30, 2005 In [Agile Related](https://osherove.com/blog/category/agile-related), [Testing Guidelines](https://osherove.com/blog/category/testing-guidelines)

**Historical article** · Originally published June 30, 2005; technical details may be outdated.

A friend asked me by email the other day :"So why do I need mock objects?" I came up with this in roughly 3 minutes so it's a bit rough, but it'll do to drive the point home, I hope. Note, that this does not really explain the \*technical\* side of it too much. Just the logic behind the concept.

Mock objects are there for when you need to replace or remove dependencies from code under test.

Let's say you have a class that manages user logins. Whenever a user login fails the class needs to report to a logger class, or maybe even an email class, or some remote web service to authenticate. If you want to write a test for the logic in the login class (an tests are just methods that call methods on the tested class, really) you want your test to run fast, and to run easily without too much fussing and configuration so that you can get fast feedback.
Also - if you replace that emailer class or authentication service class with a "fake" one you can mimic various scenarios of failures which are hard to recreate in real life or would take LOT of code. An example would be "Authentication fails/succeeds for user name". Without faking it, you'd need a lot of configuration to test that you class indeed handless success of login failure correctly each time. 

Mocks are **spies in disguise** for you tests - double agents. they let you do what you want without your real code knowing about it, and tell you everything that happened to them like _"your class \*should\* have called my 'authenticate' method with param x and Y but it actually called it with the wrong value.. Your test should \*fail\*".
_

## Lessons on this topic

- [Introduction to Hand Written Fakes, Mocks and Stubs](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/introduction-to-hand-written-fakes-mocks-and-stubs/) · The Art of Unit Testing & TDD Master Class in .NET
- [Day 1 Intro](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/day-1-intro/) · TDD and BDD in Ruby
- [Mocks, Stubs and Fakes](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/mocks-stubs-and-fakes/) · TDD and BDD in Ruby
- [Mocks And Stubs](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/mocks-and-stubs/) · The Art of Unit Testing and TDD in Java
- [Terminology](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/terminology/) · The Art of Unit Testing and TDD in Java

## More in Agile Related

[All posts in Agile Related](https://osherove.com/blog/category/agile-related)

- Jul 10, 2005

    ### [Performance vs. Testability and semi-virtual method calls](https://osherove.com/blog/2005/7/10/performance-vs-testability-and-semi-virtual-method-calls.html)

- Jul 1, 2005

    ### [Rhino.Mocks v. 2.0 Announced. Yes, it's a Mock Object Framework.](https://osherove.com/blog/2005/7/1/rhinomocks-v-20-announced-yes-its-a-mock-object-framework.html)

- Jun 29, 2005

    ### [TDD workshop today - CANCELED](https://osherove.com/blog/2005/6/29/tdd-workshop-today-canceled.html)

- Jun 26, 2005

    ### [Tomorrow on Agile Israel - some very cool talks](https://osherove.com/blog/2005/6/26/tomorrow-on-agile-israel-some-very-cool-talks.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
