Hallucination to Consensus: Multi-Agent LLMs for End-to-End JUnit Test Generation
This paper introduces CANDOR, a novel prompt engineering-based multi-agent LLM framework that leverages consensus-driven reasoning and a dual-LLM pipeline to generate high-quality, hallucination-resistant JUnit tests in Java, outperforming existing fine-tuning and search-based approaches in oracle correctness and mutation scores.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are a chef trying to teach a robot to cook a complex dish. You give the robot a recipe (the Natural Language Description) and a set of instructions on how to chop vegetables (the Source Code).
Your goal is for the robot to write a Test: a checklist that proves the dish is cooked correctly.
- The Test Prefix: The robot setting up the ingredients (e.g., "Put 5 carrots in the pot").
- The Test Oracle: The robot checking the result (e.g., "If the carrots are soft, the dish is done").
The problem? Robots (specifically, AI models called LLMs) are brilliant but prone to hallucinations. Sometimes, they confidently write a checklist that says, "If the carrots are raw, the dish is done," because that's what the current (buggy) code does, not what the recipe says should happen.
This paper introduces CANDOR, a new system that acts like a team of expert chefs working together to fix this problem. Instead of one lonely robot trying to do everything, CANDOR uses a "committee" approach.
Here is how CANDOR works, broken down into simple steps:
1. The Setup: A Team of Specialists
In the past, one AI tried to do everything: write the code, check the coverage, and fix the mistakes. This often led to confusion. CANDOR splits the job into a panel of specialized agents, like a kitchen brigade:
- The Initializer: The "Apprentice." It tries to write the very first draft of the test. It often makes syntax errors (like using Python brackets
[ ]instead of Java lists), but it gets the ball rolling. - The Planner: The "Head Chef." It looks at the code and says, "We missed the part where we test empty bowls. Let's add a test for that." It creates a game plan to cover every corner of the kitchen.
- The Tester: The "Line Cook." It follows the Planner's instructions to write the actual test code.
- The Inspector: The "Quality Control." It tries to run the code. If there's a typo or a missing ingredient (import statement), it sends it back to the Tester to fix.
2. The Problem: The "Buggy Code" Trap
Here is the tricky part. If the original recipe (the source code) has a mistake—like telling the robot to "burn the carrots"—the robot will happily write a test that says, "Burnt carrots = Success." This is called a Regression Oracle. It's useless because it just confirms the bug exists.
We need the robot to ignore the buggy code and follow the intended recipe instead.
3. The Solution: The "Panel Discussion" (The Magic Sauce)
This is where CANDOR gets creative. To fix the "burnt carrot" mistake, it doesn't just ask one AI. It sets up a Panel Discussion.
- The Panelists (Reasoning AIs): Imagine three brilliant, over-thinking food critics. They read the recipe and the buggy code. They argue back and forth.
- Panelist 1: "Wait, the code says burn them, but the recipe says roast them. The code is wrong!"
- Panelist 2: "I agree, but let me double-check the math..." (They might get lost in their own thoughts, a problem called "overthinking").
- The Interpreters: These are like secretaries for the critics. The critics are too wordy and ramble on for hours. The secretaries listen to the rambling, cut out the fluff, and write down just the key conclusion: "The carrots should be roasted, not burnt."
- The Curator: The Judge. The Judge listens to all the secretaries. If 2 out of 3 agree that the carrots should be roasted, the Judge makes the final call: "The test must check for roasted carrots."
This "Consensus" approach stops the AI from hallucinating. Even if one AI gets confused, the group corrects it.
4. The Results: Why It Matters
The researchers tested CANDOR on two big sets of coding problems (like a coding school exam).
- Coverage: CANDOR is just as good as the best existing tools (like EvoSuite) at making sure the test touches every part of the code.
- Finding Bugs: CANDOR is much better at finding actual bugs. While other tools just check if the code runs, CANDOR checks if the code does what it should do.
- Beating the Experts: It beat the current state-of-the-art tool (TOGLL) by a huge margin (over 21%). The best part? TOGLL had to be "trained" on massive amounts of data (like a student studying for years), while CANDOR just uses smart prompts (like a smart student who knows how to ask the right questions).
The Big Takeaway
CANDOR proves that you don't need to train a super-expensive AI to write perfect tests. Instead, you can use a team of standard AIs working together, arguing, and reaching a consensus.
It's the difference between asking one person to solve a math problem (who might make a mistake) and asking a whole classroom to solve it, discuss their answers, and vote on the correct one. The result is a test suite that is not only thorough but actually tells you if your software is working as intended.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.