可执行但不忠实:大型语言模型与编码智能体生成模拟对象、桩模块及断言
Executable but Unfaithful: Mock Object, Stub, and Assertion Generation by LLMs and Coding Agents
AI总结:
该研究构建MockBench基准,评估6种LLMs和3种编码智能体生成模拟对象、桩模块及断言的能力,发现编码智能体可提升可执行性但未同步提高忠实度,还识别出10种错误类型并提供相关指导。
摘要:
模拟(Mocking)是单元测试中的基础实践,使开发者能够通过用模拟替代物替换真实依赖项来隔离被测代码。自动化生成模拟对象、桩模块(Stub)及模拟断言长期以来都是研究目标。然而,捕获-重放方法依赖于记录真实依赖项的运行时行为;StubCoder等进化方法虽克服了这一要求,但将生成过程限制在预定义符号池中。大型语言模型(LLMs)与编码智能体提供了另一种方案,可从源代码生成模拟制品,无需依赖记录的依赖项执行或固定符号池。但目前尚无系统研究评估其在全部三类制品上的能力。为填补这一空白,我们构建了MockBench基准,包含来自真实Java与Python项目的165项模拟制品生成任务,且具备经验证的参考模拟对象、桩模块及模拟断言。我们从可执行性、行为忠实度及交互故障检测能力三个维度评估了6种LLMs和3种编码智能体,并与现有工具进行对比,同时开展了敏感性分析。实验结果显示,LLMs与编码智能体确实已成为基于模拟的测试领域的最先进技术。更具体而言,与LLMs相比,编码智能体大幅提升了可执行性,但这些提升并未一致转化为更高的行为忠实度或更好的交互故障检测能力。此外,我们分析了来自LLMs与编码智能体的660个失败案例,在三类制品中识别出10种错误类型,揭示了通过测试可能掩盖的行为偏差。我们的发现凸显了模拟制品生成面临的挑战,并为开发者与研究者提供了实用指导。
英文摘要:
Mocking is a foundational practice in unit testing, enabling developers to isolate the code under test by replacing real dependencies with simulated alternatives. Automating the generation of mock objects, stubs, and mock assertions has been a long-standing research goal. However, capture-and-replay approaches rely on recording the runtime behavior of actual dependencies. Evolutionary approaches such as StubCoder overcome this requirement but constrain generation to predefined symbol pools. Large language models (LLMs) and coding agents offer an alternative by generating mocking artifacts from source code without requiring recorded dependency executions or fixed symbol pools. Yet, no systematic study has evaluated their capabilities across all three artifact types. To address this gap, we construct MockBench, a benchmark comprising 165 mock artifact generation tasks from real-world Java and Python projects, with validated reference mock objects, stubs, and mock assertions. We evaluate six LLMs and three coding agents in terms of executability, behavior fidelity, and interaction-fault detection capability, complemented by comparisons with existing tools and sensitivity analysis. Experimental results show that LLMs and coding agents indeed have been the state-of-the-art techniques in mock-based testing. More specifically, compared with LLMs, coding agents substantially improve executability, while these gains do not consistently translate into higher behavior fidelity or better interaction-fault detection. Furthermore, we analyze 660 failures from LLMs and coding agents, and identify ten error types across the three artifact categories, revealing behavioral deviations that passing tests can conceal. Our findings highlight the challenges of mock artifact generation and provide practical guidance for both developers and researchers.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。