---
title: "Mock Objects vs. \"Inherit & Override\""
url: "https://osherove.com/blog/2005/6/16/mock-objects-vs-quotinherit-amp-overridequot.md"
canonical: "https://osherove.com/blog/2005/6/16/mock-objects-vs-quotinherit-amp-overridequot.html"
published: "2005-06-16T09:09:00"
updated: "2005-06-16T09:09:00"
tags: ["Agile Related"]
author: "Roy Osherove"
description: "In regards to my previous post about Mock Objects and their inherent problems, Scott writes this comment: \"I particularly like the technique where a virtual method allows the unit tests to subclass and inject…"
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/"
---

# Mock Objects vs. "Inherit & Override"

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

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

In regards to [my previous post about Mock Objects](http://weblogs.asp.net/rosherove/archive/2005/06/15/412816.aspx) and their inherent problems, Scott writes this comment:

_"I particularly like the technique where a virtual method allows the unit tests to subclass and inject dependencies. I feel this is an elegant solution compared to dependency injection with constructors. I was wondering if you had rules of thumb on when to use one versus the other? "_

**"Inherit & Override"** is a great method I use especially in these cases:

1.      When there is complex logic (usually validation logic including many "flags" in a method. I then delegate each "question" in the method to a virtual method which allows me to simulate very complex logical situations and see whether the method logic behaves correctly.
2.      When dealing with legacy code this is a great way to break dependencies without introducing new interfaces and still allowing a "poor man's" interaction testing. The trick here is to make sure that you only put in the virtual function the smallest piece of code that talks to an outside dependency without any logic. This could be a mere call to a Singleton object (making a function called "getObject" virtual ) or a call to "mailer.SendEmail(param1,param2)".
3.     When trying to remove any performance bottlenecks from the application. For example, an application that times out on specific calls, I would either remove the calls, or change the timeout duration by a parameter or an overridden method that returns the duration required.

Overall - this is a great way to break dependencies in your tests and people should be aware of it. Mock objects are not everything. However, it usually requires more code but allows for pretty elegant tests to be created.

**More resources:**

- I've demonstrated a few techniques relating to inherit and override at the [Agile Israel](http://www.agileisrael.com/) user group. The download is available [including code sample over here](http://www.agileisrael.com/LinkClick.aspx?link=58&mid=362).
- Michael Feathers' book "[Working effectively with Legacy code](https://www.amazon.com/exec/obidos/ASIN/0131177052/iserializable-20)" is a great resource with many examples of deep investigations in to the world of "how do I bend my code to make it testable?"

## Lessons on this topic

- [Extract And Override Focus](https://courses.osherove.com/courses/legacy-code-hero/extract-and-override-focus/) · Legacy Code Hero
- [Using Extract & Override](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/using-extract-and-override/) · The Art of Unit Testing & TDD Master Class in .NET
- [Extract And Override Refactoring](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/extract-and-override-refactoring/) · The Art of Unit Testing and TDD in Java
- [Legacy Refactoring intro](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/legacy-refactoring-intro/) · TDD in .NET in Hebrew
- [Static Forwarding on a real legacy app](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/static-forwarding-on-a-real-legacy-app/) · The Art of Unit Testing & TDD Master Class in .NET

## More in Agile Related

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

- Jun 17, 2005

    ### [Write clearer more powerful tests with the unofficial NMock 2 Drop](https://osherove.com/blog/2005/6/17/write-clearer-more-powerful-tests-with-the-unofficial-nmock.html)

- Jun 16, 2005

    ### [A good tester](https://osherove.com/blog/2005/6/16/a-good-tester.html)

- Jun 15, 2005

    ### [Mock Object testing can cause trouble - here's why and possibly how to avoid it](https://osherove.com/blog/2005/6/15/mock-object-testing-can-cause-trouble-heres-why-and-possibly.html)

- Jun 13, 2005

    ### [Test event logic easily with the EventsVerifier class](https://osherove.com/blog/2005/6/13/test-event-logic-easily-with-the-eventsverifier-class.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/
