Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:01:03+00:00 2026-05-16T18:01:03+00:00

I am new to unit testing, and I continously hear the words ‘mock objects’

  • 0

I am new to unit testing, and I continously hear the words ‘mock objects’ thrown around a lot. In layman’s terms, can someone explain what mock objects are, and what they are typically used for when writing unit tests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:01:04+00:00Added an answer on May 16, 2026 at 6:01 pm

    Since you say you are new to unit testing and asked for mock objects in “layman’s terms”, I’ll try a layman’s example.

    Unit Testing

    Imagine unit testing for this system:

    cook <- waiter <- customer
    

    It’s generally easy to envision testing a low-level component like the cook:

    cook <- test driver
    

    The test driver simply orders different dishes and verifies the cook returns the correct dish for each order.

    Its harder to test a middle component, like the waiter, that utilizes the behavior of other components. A naive tester might test the waiter component the same way we tested the cook component:

    cook <- waiter <- test driver
    

    The test driver would order different dishes and ensure the waiter returns the correct dish. Unfortunately, that means that this test of the waiter component may be dependent on the correct behavior of the cook component. This dependency is even worse if the cook component has any test-unfriendly characteristics, like non-deterministic behavior (the menu includes chef’s surprise as an dish), lots of dependencies (cook won’t cook without his entire staff), or lot of resources (some dishes require expensive ingredients or take an hour to cook).

    Since this is a waiter test, ideally, we want to test just the waiter, not the cook. Specifically, we want to make sure the waiter conveys the customer’s order to the cook correctly and delivers the cook’s food to the customer correctly.

    Unit testing means testing units independently, so a better approach would be to isolate the component under test (the waiter) using what Fowler calls test doubles (dummies, stubs, fakes, mocks).

        -----------------------
       |                       |
       v                       |
    test cook <- waiter <- test driver
    

    Here, the test cook is “in cahoots” with the test driver. Ideally, the system under test is designed so that the test cook can be easily substituted (injected) to work with the waiter without changing production code (e.g. without changing the waiter code).

    Mock Objects

    Now, the test cook (test double) could be implemented different ways:

    • a fake cook – a someone pretending to be a cook by using frozen dinners and a microwave,
    • a stub cook – a hot dog vendor that always gives you hot dogs no matter what you order, or
    • a mock cook – an undercover cop following a script pretending to be a cook in a sting operation.

    See Fowler’s article for the more specifics about fakes vs stubs vs mocks vs dummies, but for now, let’s focus on a mock cook.

        -----------------------
       |                       |
       v                       |
    mock cook <- waiter <- test driver
    

    A big part of unit testing the waiter component focuses on how the waiter interacts with the cook component . A mock-based approach focuses on fully specifying what the correct interaction is and detecting when it goes awry.

    The mock object knows in advance what is supposed to happen during the test (e.g. which of its methods calls will be invoked, etc.) and the mock object knows how it is supposed to react (e.g. what return value to provide). The mock will indicate whether what really happens differs from what is supposed to happen. A custom mock object could be created from scratch for each test case to execute the expected behavior for that test case, but a mocking framework strives to allow such a behavior specification to be clearly and easily indicated directly in the test case.

    The conversation surrounding a mock-based test might look like this:

    test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response

    test driver (posing as customer) to waiter: I would like a hot dog please
    waiter to mock cook: 1 hot dog please
    mock cook to waiter: order up: 1 hot dog ready (gives dummy hot dog to waiter)
    waiter to test driver: here is your hot dog (gives dummy hot dog to test driver)

    test driver: TEST SUCCEEDED!

    But since our waiter is new, this is what could happen:

    test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response

    test driver (posing as customer) to waiter: I would like a hot dog please
    waiter to mock cook: 1 hamburger please
    mock cook stops the test: I was told to expect a hot dog order!

    test driver notes the problem: TEST FAILED! – the waiter changed the order

    or

    test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response

    test driver (posing as customer) to waiter: I would like a hot dog please
    waiter to mock cook: 1 hot dog please
    mock cook to waiter: order up: 1 hot dog ready (gives dummy hot dog to waiter)
    waiter to test driver: here is your french fries (gives french fries from some other order to test driver)

    test driver notes the unexpected french fries: TEST FAILED! the waiter gave back wrong dish

    It may be hard to clearly see the difference between mock objects and stubs without a contrasting stub-based example to go with this, but this answer is way too long already 🙂

    Also note that this is a pretty simplistic example and that mocking frameworks allow for some pretty sophisticated specifications of expected behavior from components to support comprehensive tests. There’s plenty of material on mock objects and mocking frameworks for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Unit Testing, and I need to mock the File static
I'm new to Unit testing and mocking objects in Python. I have a function
I'm new to unit testing and PHPUnit, but I've reading a lot lately about
I'm new with unit testing in visual studio and I want to load a
I am new to unit testing and TDD and mocking in general, however the
I'm new in unit testing so may be I do something wrong. I have
I am new to unit testing, and would like to know how you would
I'm new to unit testing and NUit in particular. I'm just typing some examples
I am new to Unit Testing and think I might have dug myself into
I'm still new to Unit testing, and specifically PHPUnit as the testing framework. Suppose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.