To Mock or Not To Mock? An Empirical Study on Mocking Practices

Davide Spadini, Maurício Aniche, Magiel Bruntink, Alberto Bacchelli

Research output: Chapter in Book/Conference proceedings/Edited volumeConference contributionScientificpeer-review

26 Citations (Scopus)
521 Downloads (Pure)


When writing automated unit tests, developers often deal with software artifacts that have several dependencies. In these cases, one has the possibility of either instantiating the dependencies or using mock objects to simulate the dependencies' expected behavior. Even though recent quantitative studies showed that mock objects are widely used in OSS projects, scientific knowledge is still lacking on how and why practitioners use mocks. Such a knowledge is fundamental to guide further research on this widespread practice and inform the design of tools and processes to improve it. The objective of this paper is to increase our understanding of which test dependencies developers (do not) mock and why, as well as what challenges developers face with this practice. To this aim, we create MOCKEXTRACTOR, a tool to mine the usage of mock objects in testing code and employ it to collect data from three OSS projects and one industrial system. Sampling from this data, we manually analyze how more than 2,000 test dependencies are treated. Subsequently, we discuss our findings with developers from these systems, identifying practices, rationales, and challenges. These results are supported by a structured survey with more than 100 professionals. The study reveals that the usage of mocks is highly dependent on the responsibility and the architectural concern of the class. Developers report to frequently mock dependencies that make testing difficult and prefer to not mock classes that encapsulate domain concepts/rules of the system. Among the key challenges, developers report that maintaining the behavior of the mock compatible with the behavior of original class is hard and that mocking increases the coupling between the test and the production code.
Original languageEnglish
Title of host publicationProceedings - 2017 IEEE/ACM 14th International Conference on Mining Software Repositories, MSR 2017
Place of PublicationLos Alamitos, CA
Number of pages11
ISBN (Electronic)978-1-5386-1544-7
Publication statusPublished - 2017
EventMSR 2017: 14th International Conference on Mining Software Repositories - Buenos Aires, Argentina
Duration: 20 May 201721 May 2017
Conference number: 14


ConferenceMSR 2017
Abbreviated titleMSR
CityBuenos Aires
Internet address


  • Testing
  • Java
  • Databases
  • Tools
  • Software systems
  • Interviews


Dive into the research topics of 'To Mock or Not To Mock? An Empirical Study on Mocking Practices'. Together they form a unique fingerprint.

Cite this