Executable but Unfaithful: Mock Object, Stub, and Assertion Generation by LLMs and Coding Agents
Hao Ning, Jiahui Dong, Zhen Yang, Guanda Yan, Yichen Sun, Xuanyu Zhu, Zheng Shen, Yifei Pei, Jacky Keung, Jie M. Zhang
Abstract
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.
Create a lesson
Related papers
Micro Operational Robustness of LLMs on Code Generation
Shared title terminologycs.SEDebalina Ghosh Paul, Hong Zhu, Ian Bayley
Characterizing and Detecting Unfaithful Work Reports by Coding Agents
Shared title terminologycs.SEWenhao Yang, Runzhi He, Minghui Zhou et al.
VerifierBench: Benchmarking the Generation of Executable Verifiers for LLM Agents
Shared title terminologycs.SEJianing Yin, Ziyi Chen, Hao Wang et al.
Better Specifications, Better Code: Fine-Tuning LLMs for Behavioral Contract Generation
Shared title terminologycs.SEBrhane Team, Tiantian Yang, Nikta Akbarpour et al.
From Candidate Admission to Task-Level Return: Auditing LLM-Generated Code under Execution Contracts
Shared title terminologycs.SEHiroyasu Usami, Keisuke Hara, Ayato Tsuboi et al.
The Role of Agentic Instruction Categories for Code Generation: A Study of Five Python Libraries
Shared title terminologycs.SEWarren Gerald Polandra, Yoshiki Higo, Raula Gaikovina Kula