MockSniffer: Characterizing and Recommending Mocking Decisions for Unit Tests
Hengcheng Zhu, Lili Wei, Ming Wen, Yepang Liu, Shing-Chi Cheung, Qin Sheng, Cui Zhou
Abstract
In unit testing, mocking is popularly used to ease test effort, reduce test flakiness, and increase test coverage by replacing the actual dependencies with simple implementations. However, there are no clear criteria to determine which dependencies in a unit test should be mocked. Inappropriate mocking can have undesirable consequences: under-mocking could result in the inability to isolate the class under test (CUT) from its dependencies while over-mocking increases the developers' burden on maintaining the mocked objects and may lead to spurious test failures. According to existing work, various factors can determine whether a dependency should be mocked. As a result, mocking decisions are often difficult to make in practice. Studies on the evolution of mocked objects also showed that developers tend to change their mocking decisions: 17% of the studied mocked objects were introduced sometime after the test scripts were created and another 13% of the originally mocked objects eventually became unmocked. In this work, we are motivated to develop an automated technique to make mocking recommendations to facilitate unit testing. We studied 10,846 test scripts in four actively maintained open-source projects that use mocked objects, aiming to characterize the dependencies that are mocked in unit testing. Based on our observations on mocking practices, we designed and implemented a tool, MockSniffer, to identify and recommend mocks for unit tests. The tool is fully automated and requires only the CUT and its dependencies as input. It leverages machine learning techniques to make mocking recommendations by holistically considering multiple factors that can affect developers' mocking decisions. Our evaluation of MockSniffer on ten open-source projects showed that it outperformed three baseline approaches, and achieved good performance in two potential application scenarios.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 939f294f-c067-4a7f-98c8-ac9f989d52aeCited by top-tier papers3
- Use of Test Doubles in Android Testing: An In-Depth InvestigationMattia Fazzini, Chase Choi, Juan Manuel Copia, Gabriel Lee et al.ICSE 2022 · 19 citations
- JavaBench: A Benchmark of Object-Oriented Code Generation for Evaluating Large Language ModelsJialun Cao, Zhiyong Chen, Jiarong Wu, Shing-Chi Cheung et al.ASE 2024 · 9 citations
- Understanding and Characterizing Mock Assertions in Unit TestsHengcheng Zhu, Valerio Terragni, Lili Wei, Shing-Chi Cheung et al.FSE 2025 · 1 citation
Related papers
- An automatic refactoring framework for replacing test-production inheritance by mocking mechanismXiao Wang, Lu Xiao, Tingting Yu, Anne Woepse et al.FSE 2021 · 15 citations
- Mock Deep Testing: Toward Separate Development of Data and Models for Deep LearningRuchira Manke, Mohammad Wardat, Foutse Khomh, Hridesh RajanICSE 2025
- Program Sketching by Automatically Generating Mocks from TestsNate F. F. Bragg, Jeffrey S. Foster, Cody Roux, Armando Solar-LezamaCAV 2021 · 4 citations
- Studying Test Annotation Maintenance in the WildDong Jae Kim, Nikolaos Tsantalis, Tse-Hsun Peter Chen, Jinqiu YangICSE 2021 · 20 citations
- UTopia: Automatic Generation of Fuzz Driver using Unit TestsBokdeuk Jeong, Joonun Jang, Hayoon Yi, Jiin Moon et al.S&P 2023
