{"id":5003268,"date":"2015-12-16T23:15:18","date_gmt":"2015-12-17T07:15:18","guid":{"rendered":"https:\/\/aadev22.local\/?post_type=aa_glossary&#038;p=5003268"},"modified":"2023-03-08T15:41:28","modified_gmt":"2023-03-08T23:41:28","slug":"mock-objects","status":"publish","type":"aa_glossary","link":"https:\/\/agilealliance.org\/glossary\/mock-objects\/","title":{"rendered":"Mock Objects"},"content":{"rendered":"<p>A technique commonly used in the context of crafting&nbsp;<a href=\"https:\/\/agilealliance.org\/glossary\/unit-test\/\">automated unit tests<\/a>. 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\u2019s invoked as expected by the objects being tested.<\/p>\n<p>For instance, the \u201cmock\u201d version of a database component will a) provide \u201ccanned\u201d 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.<\/p>\n<h2>Also Known As<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>The generic term \u201c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Test_double\">test double<\/a>\u201d has been proposed to refer to any such component (an allusion to \u201cstunt double\u201d).<\/p>\n<h2>Expected Benefits<\/h2>\n<p>Mock objects afford \u201cdecoupling\u201d software components for the purposes of testing. For instance, code that is relied upon should not be \u201ccoupled\u201d 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.<\/p>\n<h2>Common Pitfalls<\/h2>\n<p>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\u2019 role as documentation, and these drawbacks are not always adequately compensated by the benefits of mock objects.<\/p>\n<h2>Origins<\/h2>\n<ul>\n<li>2000: the \u201cmock objects\u201d testing technique is described by Freeman, McKinnon, and Craig in their article \u201c<a href=\"http:\/\/dl.acm.org\/citation.cfm?id=377534\">Endo-Testing: Unit Testing with Mock Objects<\/a>\u201c, an allusion to the \u201cMock Turtle\u201d character in Lewis Carroll<\/li>\n<li>2010: a comprehensive description of integrating mock objects, TDD, and OO design is provided in Freeman and Pryce\u2019s \u201c<a href=\"http:\/\/www.growing-object-oriented-software.com\/\">Growing Object-Oriented Software Guided by Tests<\/a>\u201c<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Mock Objects (commonly used in the context of crafting automated unit tests) consist of instantiating a test-specific version of a software component.<\/p>\n","protected":false},"author":8027401,"featured_media":8067461,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","categories":[906],"tags":[],"class_list":["post-5003268","aa_glossary","type-aa_glossary","status-publish","has-post-thumbnail","hentry","category-technology"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/aa_glossary\/5003268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/aa_glossary"}],"about":[{"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/types\/aa_glossary"}],"author":[{"embeddable":true,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/users\/8027401"}],"replies":[{"embeddable":true,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/comments?post=5003268"}],"version-history":[{"count":0,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/aa_glossary\/5003268\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/media\/8067461"}],"wp:attachment":[{"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/media?parent=5003268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/categories?post=5003268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agilealliance.org\/wp-json\/wp\/v2\/tags?post=5003268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}