---
title: "Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test"
url: "https://osherove.com/blog/2008/4/24/injecting-typemock-stubs-and-mocks-using-the-structuremap-co.md"
canonical: "https://osherove.com/blog/2008/4/24/injecting-typemock-stubs-and-mocks-using-the-structuremap-co.html"
published: "2008-04-24T16:40:23"
updated: "2008-04-24T16:40:23"
tags: ["Art Of Unit Testing", "Testing Guidelines", "Typemock", "Unit Testing"]
author: "Roy Osherove"
description: "Someone asked for examples of how you'd inject stubs and mocks when you'd like your object under test to use a container such as StructureMap . here are a couple of simple tests to show this using Typemock Isolator .…"
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/"
---

# Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

By Roy Osherove April 24, 2008 In [Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing), [Testing Guidelines](https://osherove.com/blog/category/testing-guidelines), [Typemock](https://osherove.com/blog/category/typemock), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published April 24, 2008; technical details may be outdated.

Someone asked for examples of how you'd inject stubs and mocks when you'd like your object under test to use a container such as [StructureMap](http://structuremap.sourceforge.net/Default.htm).

here are a couple of simple tests to show this using [Typemock Isolator](http://www.typemock.com/).

the first uses the string based mocks found in the free community version of Typemock Isolator. the second uses the natural style syntax in the enterprise edition:

- assume you have ILogger, IEmailer interfaces and a MessageManager class under test that takes both of these in the ctor.
- The logic under test is: if calling the emailer throws an exception, the logger should be called with the message that was sent

**\[Test, VerifyMocks\]
public void** **MessageManager\_FailsEmailing\_LogsMessage\_ReflectiveStructureMap()**
{
    StructureMapConfiguration.BuildInstancesOf<IEmailer>().AddConcreteType<Emailer>();
    StructureMapConfiguration.BuildInstancesOf<ILogger>().AddConcreteType<Logger>();

    MockObject<IEmailer> emailStub = MockManager.MockObject<IEmailer>();
    emailStub.AlwaysThrow("Send", new Exception("fake exception"));

    MockObject<ILogger> logMock = MockManager.MockObject<ILogger>();
    logMock.ExpectCall("Write").Args("could not email message abc");

    ObjectFactory.InjectStub(emailStub.MockedInstance);
    ObjectFactory.Inject(logMock.MockedInstance);

    MessageManager mm = ObjectFactory.GetInstance<MessageManager>();
    mm.SendMessage("abc");
}

\----------------------------------------

**\[Test, VerifyMocks\]
public void MessageManager\_FailsEmailing\_LogsMessage\_NaturalStructureMap()
**{
    StructureMapConfiguration.BuildInstancesOf<IEmailer>().AddConcreteType<Emailer>();
    StructureMapConfiguration.BuildInstancesOf<ILogger>().AddConcreteType<Logger>();

    IEmailer emailStub = RecorderManager.CreateMockedObject<IEmailer>();
    ILogger logMock = RecorderManager.CreateMockedObject<ILogger>();

    using (RecordExpectations re = RecorderManager.StartRecording())
    {
        emailStub.Send("", "", "", "");
        re.Throw(new Exception("fake exception")).RepeatAlways();

        logMock.Write("could not email message abc");
    }

    ObjectFactory.InjectStub(emailStub);
    ObjectFactory.Inject(logMock);

    MessageManager mm = ObjectFactory.GetInstance<MessageManager>();
    mm.SendMessage("abc");
}

## More in Art Of Unit Testing

[All posts in Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing)

- Apr 25, 2008

    ### [Pros and Cons and of using an Auto Mocking container in your tests](https://osherove.com/blog/2008/4/25/pros-and-cons-and-of-using-an-auto-mocking-container-in-your.html)

- Apr 24, 2008

    ### [Testing XML serialization attributes](https://osherove.com/blog/2008/4/24/testing-xml-serialization-attributes.html)

- Mar 1, 2008

    ### [What is a good example of unit test over specification?](https://osherove.com/blog/2008/3/1/what-is-a-good-example-of-unit-test-over-specification.html)

- Feb 14, 2008

    ### [Future of Mock Frameworks: AEIS - Abstract Expectations & Isolation Syntax](https://osherove.com/blog/2008/2/14/future-of-mock-frameworks-aeis-abstract-expectations-amp-iso.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/
