Generate Java Unit Tests with Codeac on GitHub Actions: A Quick Guide
Discover how to auto-generate unit tests for Java projects using Codeac’s GitHub Actions integration, cut manual effort, and improve coverage—step‑by‑step with real examples.
07 Jul 2026, 14:26 UTC

Why Auto‑Generate Tests? The Core Problem
Writing unit tests for every new feature is time‑consuming and error‑prone. In many teams, test coverage lags behind code changes, leaving hidden bugs. Codeac’s AI can analyze your Java source and produce boilerplate JUnit tests automatically, potentially cutting test‑writing effort by up to 70% in typical scenarios.
How Codeac Fits Into a GitHub Workflow
Codeac is delivered as a reusable GitHub Action. Once configured, it runs in your CI pipeline, generates tests, and commits them back to the repo for review. The integration requires only two files:
.github/workflows/codeac.yml– the workflow definition..codeac.yml– a minimal configuration that tells Codeac what to generate.
1. Create the Codeac Configuration
Place a .codeac.yml file at the root of your repository. The following example instructs Codeac to generate unit tests for all Java classes in src/main/java:
# .codeac.yml
# Target Java source directory
source:
- src/main/java
# Goal: generate JUnit 5 tests
goal: generate-unit-tests
# Optional: add a prompt to improve relevance
prompt: "Generate JUnit 5 tests that cover public methods of the following classes."
2. Store the API Key Securely
Obtain an API key from the Codeac dashboard and add it as a GitHub secret named CODEAC_API_KEY. Never expose this key in logs or public files.
3. Define the GitHub Actions Workflow
Create .github/workflows/codeac.yml with the following content:
name: Codeac Test Generation
on:
pull_request:
branches: [main]
schedule:
- cron: '0 2 * * *' # Every day at 02:00 UTC
jobs:
generate-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Generate unit tests
uses: codeac/codeac@v1
with:
api-key: ${{ secrets.CODEAC_API_KEY }}
config-file: .codeac.yml
- name: Commit generated tests
if: ${{ github.event_name == 'pull_request' }}
run: |
git config --global user.email "[contact removed]"
git config --global user.name "CI Bot"
git add src/test/java
git commit -m "chore: add AI‑generated unit tests"
git push
Key points:
- The action runs on pull requests and nightly schedules.
- By default, it writes tests to
src/test/javaand commits them if the workflow is triggered by a PR. - All commands execute with the permissions granted to the workflow runner.
Practical Example: From Source to Coverage
Suppose you add a new class Calculator.java:
package com.example;
public class Calculator {
public int add(int a, int b) { return a + b; }
public int subtract(int a, int b) { return a - b; }
}
After pushing the file, the workflow triggers Codeac. The AI reads the class, generates two JUnit 5 tests, and commits them:
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAdd() { assertEquals(5, new Calculator().add(2, 3)); }
@Test
void testSubtract() { assertEquals(-1, new Calculator().subtract(2, 3)); }
}
After the commit, run a coverage tool such as JaCoCo to verify the impact. A typical coverage table might look like:
| Metric | Before | After |
|---|---|---|
| Line Coverage | 45% | 68% |
| Branch Coverage | 30% | 55% |
These numbers are illustrative; your results will vary based on the codebase.
Trade‑Offs and Caveats
- Missing Edge Cases: AI‑generated tests may overlook uncommon scenarios. Always review generated tests for completeness.
- Prompt Clarity: Ambiguous or missing prompts can lead to irrelevant tests. Include a concise prompt in
.codeac.ymlto guide the model. - Secret Management: Storing the API key as a GitHub secret is mandatory. If the key leaks into workflow logs, revoke and rotate it immediately.
- Commit Overwrites: The action commits tests by default. If you prefer manual commits, disable the commit step and merge the generated files manually.
Actionable Checklist
- Set up
.codeac.ymlwith target directories and a clear prompt. - Store your Codeac API key as
CODEAC_API_KEYin GitHub secrets. - Add the workflow file and verify it runs on a test PR.
- Review the committed tests, adjust naming or assertions as needed.
- Run a coverage tool to confirm that test coverage has improved.
- Iterate: tweak the prompt or configuration if coverage goals are not met.
By following these steps, you can harness Codeac’s AI to accelerate test development while maintaining quality control through review and coverage checks.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.