Agile Glossary

Mock Objects

What is Mock Objects?

A technique commonly used in the context of crafting automated unit tests. It consists of instantiating a test-specific version of a software component (typically a class), which instead of the normal behaviors provides pre-computed results, and often also checks that it’s invoked as expected by the objects being tested.

For instance, the “mock” version of a database component will a) provide “canned” answers to database queries, instead of connecting to a real live database, and b) verify that the database is being accessed in the manner expected and stipulated in the test.

Also Known As

There are many terms for components that mimic actual code for the purposes of testing: mocks, stubs, fakes, spies, and dummies to name the more common.

These terms are not strict synonyms, various authors make relatively subtle distinctions between these categories, based on whether the components have behavior, record interactions with other components, etc.

The generic term “test double” has been proposed to refer to any such component (an allusion to “stunt double”).

Expected Benefits

Mock objects afford “decoupling” software components for the purposes of testing. For instance, code that is relied upon should not be “coupled” to the implementation details of that database: its correct function relies on the logical organization of the data being queried, rather than on concrete aspects such as storing data on disk or establishing connections over network protocols. Mock objects lead to tests that only exercise the behavior of the software components under test, and do not depend on the behavior of other, unrelated components.

Common Pitfalls

The technique is slightly controversial, having both adepts and detractors; the latter argue that overuse of mock objects leads to a heavier test maintenance burden, and detracts from the tests’ role as documentation, and these drawbacks are not always adequately compensated by the benefits of mock objects.

Origins

Join us today!

Agile Alliance offers many online and in-person events and workshops for our members. If you’re not currently a member, you can join now to take advantage of our many members-only resources and programs. LEARN MORE >

Get the latest Agile news!

  • This field is for validation purposes and should be left unchanged.

By subscribing, you acknowledge the Agile Alliance Privacy Policy, and agree to receive our emails.

Additional Agile Glossary Terms

The product owner is a role on a product development team responsible for managing the product backlog in order to achieve the desired outcome that a product development team seeks to accomplish.
Collective code ownership is the explicit convention that every team member can make changes to any code file as necessary: either to complete a development task, to repair a defect, or to improve the code's overall structure.
A team adopting the "simple design" practice bases its software design strategy on a set of "simple design" principles.
In an Agile context, Incremental Development is when each successive version of a product is usable, and each builds upon the previous version by adding user-visible functionality.
Definition of Ready involves creating clear criteria that a user story must meet before being accepted into an upcoming iteration. This is typically based on the INVEST matrix.
A technique to scale Scrum up to large groups (over a dozen people), consisting of dividing the groups into Agile teams of 5-10.
Extreme Programming (XP) is an agile software development framework that aims to produce higher quality software, and higher quality of life for the development team. XP is the most specific of the agile frameworks regarding appropriate engineering practices for software development.

Help us keep the definitions updated

Ready to join Agile Alliance?

Unlock members-only access to online learning sessions, Agile resources, annual conference discounts, and more! And when you join, you’ll be supporting our member initiatives, regional events, and global community groups.

Privacy Preference Center

IMPORTANT: We have transitioned to a new membership platform. If you have not already done so, you will need to SET UP AN ACCOUNT on the new platform to establish your user profile. Your previous login credentials will not work until you do this set up.

When you see the login screen, choose “Set up Account” and follow the prompts to create your new account. You can choose to log in using your social credentials for either Google or Linkedin (recommended), or you can set up your account using an email address.