[Paper Review] Using Large Language Models to Generate JUnit Tests: An Empirical Study
This study evaluates Codex, GPT-3.5-Turbo, and StarCoder for zero-shot JUnit5 test generation on Java, analyzing compilation, correctness, coverage, and test smells across HumanEval and SF110 benchmarks, plus the impact of different context styles.
A code generation model generates code by taking a prompt from a code comment, existing code, or a combination of both. Although code generation models (e.g., GitHub Copilot) are increasingly being adopted in practice, it is unclear whether they can successfully be used for unit test generation without fine-tuning for a strongly typed language like Java. To fill this gap, we investigated how well three models (Codex, GPT-3.5-Turbo, and StarCoder) can generate unit tests. We used two benchmarks (HumanEval and Evosuite SF110) to investigate the effect of context generation on the unit test generation process. We evaluated the models based on compilation rates, test correctness, test coverage, and test smells. We found that the Codex model achieved above 80% coverage for the HumanEval dataset, but no model had more than 2% coverage for the EvoSuite SF110 benchmark. The generated tests also suffered from test smells, such as Duplicated Asserts and Empty Tests.
Motivation & Objective
- Assess how well zero-shot LLMs can generate JUnit tests for Java classes.
- Examine the effect of different context inputs (e.g., JavaDocs, method signatures) on test generation performance.
- Compare LLM-generated tests with Evosuite in terms of compilation, correctness, coverage, and smell indicators.
- Characterize common test smells in LLM-generated tests and their prevalence.
- Discuss implications for test-driven development using code generation models.
Proposed method
- Use three LLMs (Codex, GPT-3.5-Turbo, StarCoder) to generate JUnit5 tests for MUTs in the Java HumanEval dataset and SF110 benchmark.
- Create prompts and contexts that include the class under test and required JUnit imports, generating one test file per testable method.
- Apply heuristic fixes to address compilation issues in generated tests and measure post-fix compilation rates.
- Evaluate tests with JaCoCo for line/branch coverage and correctness based on pass rates, assuming correct production code.
- Detect test smells with TsDetect across generated tests and Evosuite/manual baselines.
- Conduct two RQ-focused experiments: RQ1 (overall test generation performance) and RQ2 (influence of context elements).
- Analyze compilation root causes via clustering of compilation errors to identify common failure modes.

Experimental results
Research questions
- RQ1RQ1: How well can LLMs generate JUnit tests for Java MUTs on the chosen benchmarks?
- RQ2RQ2: How do different context elements in prompts influence LLM performance in test generation?
- RQ3How do LLM-generated tests compare to Evosuite in terms of compilation, correctness, coverage, and smells?
Key findings
- Codex achieved above 80% coverage on HumanEval, but no model exceeded 2% coverage on SF110 prior to fixes.
- After applying heuristics, compilation rates increased on average by 41% across datasets.
- StarCoder produced the highest compilable test proportion on SF110 (12.7%), but improvements from fixes varied by model.
- On HumanEval, 70% of StarCoder tests compiled after fixes; Codex (2K) improved from 37.5% to 100% compilable after fixes; Codex (4K) improved from 44.4% to 99.4%.
- For SF110, compilation rates were generally low (2.7%–12.7% pre-fix), increasing by about 81% after fixes; Evosuite achieved 100% compilable by design.
- Correctness rates were modest: StarCoder about 81% correct on HumanEval; others ranged 41–77% with Codex variants; SF110 correctness was lower (best ~51.9% correct by StarCoder).
- HumanEval coverage: Codex (4K) achieved 87.7% line and 92.8% branch coverage; Evosuite and manual tests showed higher coverage in some cases.
- SF110 coverage was substantially lower (≤~2%) across LLMs, with Evosuite outperforming LLMs by large margins.
- Test smells were prevalent in LLM-generated tests, especially Magic Number (MNT) and Assertion Roulette (AR); StarCoder showed particularly high smell incidence in SF110 (96.7% with at least one smell).
- Context scenarios (JavaDoc presence, implementation availability) influenced compilation and correctness; GPT-3.5-Turbo often showed sharp declines with certain scenario configurations, while Codex and StarCoder responded differently.

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.