[Paper Review] Mimicking Production Behavior with Generated Mocks
This paper proposes rick, an open-source tool that generates realistic unit tests with mock-based oracles by monitoring real production executions. It instruments target methods, captures contextual data including inputs, returns, and interactions, and uses this to create executable tests that mimic production behavior with 52.4% success in replicating full execution contexts.
Mocking allows testing program units in isolation. A developer who writes tests with mocks faces two challenges: design realistic interactions between a unit and its environment; and understand the expected impact of these interactions on the behavior of the unit. In this paper, we propose to monitor an application in production to generate tests that mimic realistic execution scenarios through mocks. Our approach operates in three phases. First, we instrument a set of target methods for which we want to generate tests, as well as the methods that they invoke, which we refer to as mockable method calls. Second, in production, we collect data about the context in which target methods are invoked, as well as the parameters and the returned value for each mockable method call. Third, offline, we analyze the production data to generate test cases with realistic inputs and mock interactions. The approach is automated and implemented in an open-source tool called RICK. We evaluate our approach with three real-world, open-source Java applications. RICK monitors the invocation of 128 methods in production across the three applications and captures their behavior. Based on this captured data, RICK generates test cases that include realistic initial states and test inputs, as well as mocks and stubs. All the generated test cases are executable, and 52.4% of them successfully mimic the complete execution context of the target methods observed in production. The mock-based oracles are also effective at detecting regressions within the target methods, complementing each other in their fault-finding ability. We interview 5 developers from the industry who confirm the relevance of using production observations to design mocks and stubs. Our experimental findings clearly demonstrate the feasibility and added value of generating mocks from production interactions.
Motivation & Objective
- Address the challenge of creating realistic mocks and test inputs for unit testing, which are often based on assumptions rather than real system behavior.
- Overcome the limitations of synthetic or manually designed mocks that may not reflect actual production interactions.
- Improve test effectiveness by leveraging real-world execution data to generate oracles that verify method interactions with external dependencies.
- Enable automated, executable test generation that preserves the actual context and behavior observed in production.
- Provide developers with test cases that reflect real usage patterns, enhancing regression detection and test maintainability.
Proposed method
- Instrument target methods and their dependencies (mockable method calls) in production for runtime data collection.
- Monitor and log contextual data during production execution, including method parameters, return values, and invocation sequences.
- Offline, analyze captured data to generate test cases with realistic initial states, inputs, and stubbed mocks.
- Construct three types of mock-based oracles: output oracle (OO), parameter oracle (PO), and call order oracle (CO) to verify interactions.
- Serialize complex object states from production to recreate realistic test environments in unit tests.
- Automate the entire pipeline from instrumentation to test generation using the open-source rick tool.
Experimental results
Research questions
- RQ1RQ1: Can rick successfully monitor and transform observed production behavior into executable test cases?
- RQ2RQ2: To what extent can captured production data be expressed as realistic test inputs, mocks, and oracles in generated tests?
- RQ3RQ3: How well do the generated tests reproduce the full execution context observed in production?
- RQ4RQ4: How effective are the three mock-based oracles (OO, PO, CO) in detecting regressions, and do they complement each other?
- RQ5RQ5: Are the generated tests and oracles considered relevant and understandable by professional developers?
Key findings
- rick successfully generated 294 test cases for 128 target methods across three real-world open-source Java applications.
- 52.4% of the generated tests successfully mimicked the complete execution context observed in production, including realistic inputs, mocks, and oracles.
- The three mock-based oracles—output (OO), parameter (PO), and call order (CO)—complement each other in detecting regressions within the target methods.
- The generated tests were evaluated as relevant and understandable by five professional developers in a qualitative survey.
- The approach enables the automatic creation of executable tests with explicit oracles derived directly from production behavior, unlike capture-and-replay techniques that rely on implicit oracles.
- The study demonstrates the feasibility of generating isolated, realistic unit tests from production data, bridging the gap between development assumptions and real system behavior.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.