paperbot · PL 论文追踪

全部论文 待读 收藏

P2 An automatic refactoring framework for replacing test-production inheritance by mocking mechanism

一种用 mocking 机制替换测试-生产代码继承的自动重构框架
FSE Proceedings of the 29th ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering2021匹配度 2/10 · 该论文属于软件测试与自动重构方向,核心是识别测试中继承式 mock 并替换为 Mockito,与用户关注的 PL/AI 交叉、静态分析、Agentic 程序、浮点程序分析或贝叶斯程序分析等方向基本无关。虽然涉及自动化代码变换和一定程序分析,但研究问题与用户画像中的核心问题距离较远。引用 16 inbox
Xiao Wang, Lu Xiao, Tingting Yu, Anne Woepse, Sunny Wong
crossref: 2026-09-12

AI 速览

一句话:提出全自动重构框架,识别单元测试中手写的继承式 mock 并改写为 Mockito 调用。

单元测试需要隔离被测单元,但软件单元之间高度耦合,开发者常用 mocking 以「伪造」对象替换依赖。尽管已有成熟的 mocking 框架,实践中开发者仍偏好「手写」方案——继承:创建依赖类的子类并通过方法覆写来模拟行为。这种做法实现繁琐,并损害单元测试的设计质量。本文提出一个全自动重构框架,自动识别这类继承式 mock 的使用并将其替换为 Mockito 机制;框架的设计建立在五个使用继承进行 mocking 的开源项目的经验之上。作者在另外四个项目上评估该方法,结果表明框架高效、对新的数据集普遍适用,在检测缺陷(以变异体形式度量)方面基本保持测试用例行为,并使测试代码与生产代码解耦。有经验的开发者所做的定性评估表明,框架生成的自动重构结果从多个方面提升了单元测试质量,例如使测试条件更显式,并改善测试用例的内聚性、可读性、可理解性与可维护性。

问题
开发者在单元测试中常通过继承依赖类并覆写方法来手写 mock,这既需要繁琐的实现,又损害测试代码的设计质量与可维护性。
方法
基于五个开源项目中使用继承做 mocking 的经验,构建全自动重构框架,自动识别此类继承用法并将其改写为 Mockito 的 mocking 机制。
评估
在另外四个开源项目上评估框架的效率与通用性,用变异体检测衡量测试行为是否保持,并考察测试代码与生产代码的解耦程度,最后邀请有经验的开发者做定性评价。
原文摘要(Abstract)

Unit testing focuses on verifying the functions of individual units of a software system. It is challenging due to the high inter-dependencies among software units. Developers address this by mocking-replacing the dependency by a "faked" object. Despite the existence of powerful, dedicated mocking frameworks, developers often turn to a "hand-rolled" approach-inheritance. That is, they create a subclass of the dependent class and mock its behavior through method overriding. However, this requires tedious implementation and compromises the design quality of unit tests. This work contributes a fully automated refactoring framework to identify and replace the usage of inheritance by using Mockito-a well received mocking framework. Our approach is built upon the empirical experience from five open source projects that use inheritance for mocking. We evaluate our approach on four other projects. Results show that our framework is efficient, generally applicable to new datasets, mostly preserves test case behaviors in detecting defects (in the form of mutants), and decouples test code from production code. The qualitative evaluation by experienced developers suggests that the auto-refactoring solutions generated by our framework improve the quality of the unit test cases in various aspects, such as making test conditions more explicit, as well as improved cohesion, readability, understandability, and maintainability with test cases.

链接与引用

DOI 原文 ·

BibTeX
@inproceedings{paperbot6421,
  title = {An automatic refactoring framework for replacing test-production inheritance by mocking mechanism},
  author = {Xiao Wang and Lu Xiao and Tingting Yu and Anne Woepse and Sunny Wong},
  booktitle = {FSE},
  year = {2021},
  doi = {10.1145/3468264.3468590}
}