Automated Detection and Refactoring of Mock Clones in Java Projects
Gengwu Zhao · 2025
Mocking is an essential technique in unit testing, allowing developers to isolate dependencies for more focused and reliable tests. However, repetitive patterns of mock creation, termed mock clones, introduce significant redundancy, increasing maintenance costs, reducing readability, and complicating updates in test code. To address these challenges, our research investigates the detection and refactoring of mock clones, aiming to improve the maintainability and efficiency of test code in large software projects. We began with an empirical study of mocking framework usage across open-source Java projects, revealing widespread adoption of frameworks like Mockito, along with patterns and practices that commonly lead to mock clones. Following this, we analyzed the prevalence and impact of mock clones across six representative projects, identifying significant redundancy and demonstrating the limitations of traditional clone detection tools. Manual refactoring of identified mock clones reduced thousands of redundant lines, with developers responding positively to these improvements. Building on these foundational studies, our ongoing work seeks to develop automated tools for detecting and refactoring mock clones, enabling systematic and scalable improvements to test code. This research offers both empirical insights and practical solutions that contribute toward automated test code maintenance, advancing sustainable software testing practices.