DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating Smart Stubs with a Mock Framework

Creating Smart Stubs with a Mock Framework

Alvin Ashcraft user avatar by
Alvin Ashcraft
·
Oct. 01, 09 · Interview
Like (0)
Save
Tweet
Share
8.84K Views

Join the DZone community and get the full member experience.

Join For Free

This article is taken from The Art of Unit Testing from Manning Publications. As part of a chapter on mock object frameworks, this segment shows how to create a stub and fake an exception with a dynamically created stub. For the table of contents, the Author Forum, and other resources, go to http://www.manning.com/osherove/.

It is being reproduced here by permission from Manning Publications. Manning ebooks are sold exclusively through Manning. Visit the book's page for more information.

Softbound print: May 2009 | 320 pages
ISBN: 1933988274

 

Use code "dzone30" to get 30% off any version of this book.

 

A stub can also be created using mocking frameworks. Semantically speaking, a stub is a simulated object, just like a mock object, that will support our test so that we can test other things. In short, a stub will always make “happy noises” when being called, and can never be used to see if the test passed or not. Calling VerifyAll() will not verify anything against stub objects. Only against mocks. Most mocking frameworks contain the semantic notion of a stub and RhinoMocks is no exception. Listing 1 shows how you create a stub object:

Listing 1: Creating a stub looks remarkably similar to creating a mock object. Only the creation method call changes.

[Test]
public void ReturnResultsFromStub()
{
MockRepository mocks = new MockRepository();
IGetRestuls resultGetter = mocks.Stub<IGetRestuls>();

using(repository.Record())
{
resultGetter.GetSomeNumber("a");
LastCall.Return(1);
}

int result = resultGetter.GetSomeNumber("a");
Assert.AreEqual(1, result);
}

The Syntax looks almost the same as using mock objects But what would happen when running a test that does not call an expected method on the stub, but still verifies expectations as shown in listing 2:

Listing 2: Verifying expectations on a stub object cannot fail a test. That’s what mock objects are for.

public void StubNeverFailsTheTest()
{
MockRepository mocks = new MockRepository();
IGetRestuls resultGetter = mocks.Stub<IGetRestuls>();

using(mocks.Record())
{
resultGetter.GetSomeNumber("a"); |#1
LastCall.Return(1);
}

resultGetter.GetSomeNumber("b");
mocks.VerifyAll(); |#2
}

Annotation #1: Is this really an expectation?
Annotation #2: Will this fail the test?

The test should still pass, because the stub, by definition, is incapable of breaking a test, and that’s also how it behaves in RhinoMocks. Any expectations set on it are purely so we can determine the return value or the exceptions to throw from them.

It’s important to note that RhinoMocks actually contains a very nice feature for simple properties on stub objects. Get-set properties are automatically implemented and can be set and used as if this was a real object, without needing to setup a return value. You can still set up a return value for a property, but you don’t need to do it for each and every property on a stub object as shown in the next piece of code:

ISomeInterfaceWithProperties stub =
mocks.Stub<ISomeInterfaceWithProperties>();
stub.Name = "Itamar";
Assert.AreEqual("Itamar",stub.Name);

Let’s see how we can simulate an exception using expectations. Listing 3 shows an example of how you would simulate an OutOfMemoryException in a test from a stub:

Listing 3: Faking an exception with a dynamically created stub is very easy and very readable when using the LastCall class.

public void StubSimulatingException()
{
MockRepository mocks = new MockRepository();
IGetRestuls resultGetter = mocks.Stub<IGetRestuls>();

using(mocks.Record())
{
resultGetter.GetSomeNumber("A");
LastCall.Throw(
new OutOfMemoryException("The system is out of memory!")
);
}

resultGetter.GetSomeNumber("A");
}

This test will fail due to a nasty out of memory exception. That’s how easy it is.

 

Stub (distributed computing) unit test Framework Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Tracking Software Architecture Decisions
  • Comparing Map.of() and New HashMap() in Java
  • The Path From APIs to Containers
  • 5 Steps for Getting Started in Deep Learning

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: