Java unit test generator
Skill timwukp/agent-skills-best-practice/skills/skills/java-unit-test-generator
35 portable agent skills (Agent Skills spec) for Kiro & Claude Code: Scrum DevSecOps roles, PCI-DSS/MAS TRM compliance, AWS Well-Architected reviews — each with evals and a 4-layer tested methodology
npx -y skills add timwukp/agent-skills-best-practice --skill java-unit-test-generatorAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 7 stars7 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Generates JUnit unit tests for Java classes with loop prevention and incremental generation. Triggers on: "generate unit tests", "create JUnit tests", "test Java class", "add test coverage".
The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.
SKILL.md
3.8 KB, as published. Nobody here has run it
Java Unit Test Generator
Instructions
Step 1: Scope Definition
Before generating tests, ask:
- Which class/method?
- Coverage goal? (Basic = happy path | Standard = + errors | Comprehensive = + edge cases)
- Framework? (JUnit 4, JUnit 5, TestNG)
- Mocking? (Mockito, EasyMock, none)
Provide an upfront estimate: 10-15 credits per method.
Step 2: Generate 3 Tests Per Batch
For each method, generate exactly 3 tests:
- Happy path
- Error handling
- Edge case
Use this structure:
@Test
@DisplayName("Should [expected behavior] when [condition]")
void test[MethodName][Scenario]() {
// Given - setup
// When - execute
// Then - verify
}
STOP after each batch. Ask the user to run and confirm before continuing.
Step 3: Validation Gate
Do NOT generate more tests until the user confirms the current batch works.
Validation command:
python scripts/validate_tests.py <TestClassName>
Step 4: Fix or Skip (Max 2 Attempts)
If tests fail:
- Attempt 1: Fix compilation errors (imports, syntax)
- Attempt 2: Fix logic errors (assertions, test data)
- After 2 failures: Skip the test, document the issue, and move on
When skipping, say:
"This test failed twice. Skipping to preserve credits. Issue: [describe]. Options: (1) skip, (2) provide more context, (3) simplify scope."
Never attempt the same fix more than twice. Mark flaky tests @Disabled.
Step 5: Iterate or Finish
After a passing batch, offer:
- 3 more tests for the same method
- Move to next method
- Stop and produce summary
Step 6: Summary Report
Methods Tested: N
Total Tests: N
Skipped: N (with reasons)
Coverage: happy path / error / edge case breakdown
Measuring Credit Cost for Your Code
Credit costs vary by Kiro account, model, and code complexity. To get accurate numbers for your environment, follow these steps.
1. Establish your baseline (one-time)
Run the built-in benchmark with a simple sample class:
python track_credits.py benchmark java-simple
This measures the cheapest possible unit test on your account (e.g., 0.063 credits/test).
2. Estimate before running on your code
Your real classes are more complex. The tool scores complexity based on:
| What makes tests cost more | How it's scored |
|---|---|
| More lines of code | +1 for every 50 lines |
| More mock dependencies | +1 for each mock |
| More branching (if/else/switch) | +1 for every 3 branches |
| More framework annotations | +1 for every 3 annotations |
python track_credits.py estimate \
--type java-unit \
--source-lines 150 \
--mocks 3 \
--branches 6 \
--annotations 4 \
--tests 3
3. Measure actual cost after running
# Note credit balance before and after, then record:
python track_credits.py add "MyService-3tests" <before> <after> "3 JUnit5 tests, Mockito"
Your measurements improve future estimates automatically.
See ../../../skills-workshop/credit-estimation/CREDIT-ESTIMATION-GUIDE.md for the full guide.
References
references/junit5-patterns.md- Common JUnit 5 patternsreferences/examples/UserServiceTest.java- Complete example../../../skills-workshop/credit-estimation/CREDIT-ESTIMATION-GUIDE.md- Credit measurement methodology- JUnit 5 User Guide
- Mockito Documentation