An AI-powered Java unit test generation agent. Automatically generates high-quality JUnit 5 + Mockito tests.
🤖 Multi-LLM Support - OpenAI, Anthropic, Gemini, Zhipu AI
🔧 Self-Healing - Auto-fixes compilation errors and test failures
📊 Coverage-Driven - Analyzes coverage and supplements missing tests
🔄 Iterative Mode - Generates tests one method at a time
✅ LSP Syntax Check - Optional semantic analysis before compile
🔄 Dynamic Phase Switching - Saves 40-60% tokens (v2.2.0)
curl -sSL https://raw.githubusercontent.com/codelogickeep/unit-test-agent-4j/main/build.sh | bash
export PATH=" $PATH :$HOME /.utagent"
irm https:// raw.githubusercontent.com / codelogickeep/ unit- test-agent - 4j/ main/ build.ps1 | iex
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Analysis │ → │ Generation │ → │Verification │ → │ Repair │
│ Read source │ │ Write tests │ │Compile/Test │ │ Fix errors │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
│ │
└────────────────┘
Loop until pass
Analysis - Read and analyze source code (AST, dependencies, complexity)
Generation - Generate test code based on patterns and knowledge base
Verification - Compile, run tests, check coverage
Repair - Auto-fix failures, repeat until success or max retries
1. Configure API Key (First Time)
# Interactive config
utagent config --protocol openai --api-key " sk-..." --model " gpt-4o"
# Or use environment variables
export UT_AGENT_API_KEY=" sk-..."
export UT_AGENT_MODEL_NAME=" gpt-4o"
# Basic
utagent --target src/main/java/com/example/MyService.java
# With knowledge base (learn from existing tests)
utagent --target src/main/java/com/example/MyService.java \
-kb src/test/java
# Interactive mode (confirm before writes)
utagent --target src/main/java/com/example/MyService.java -i
# Custom coverage threshold
utagent --target src/main/java/com/example/MyService.java --threshold 90
3. Batch Mode (Entire Project)
# Scan and test all uncovered classes
utagent --project /path/to/project
# With exclusions
utagent --project /path/to/project \
--exclude " **/dto/**,**/vo/**,**/entity/**"
# Dry-run (analyze only)
utagent --project /path/to/project --dry-run
Option
Short
Description
Default
--target <file>
-t
Target Java source file
-
--project <dir>
-p
Project root for batch mode
-
--config <file>
-c
Config file path
auto-detect
--threshold <n>
Coverage threshold (0-100)
80
--interactive
-i
Confirm before file writes
false
--dry-run
Analyze only, no generation
false
--check-env
Check environment and exit
-
-v
Verbose logging
false
-h, --help
Show help
-
Option
Description
Example
--protocol
LLM protocol
openai, anthropic, gemini, openai-zhipu
--api-key
API key
sk-...
--base-url
API base URL
https://api.openai.com
--model
Model name
gpt-4o, claude-3-5-sonnet
--temperature
Sampling temperature
0.0 - 1.0
--max-retries
Max retry attempts
5
--save
Save overrides to config
-
Option
Description
Example
--exclude
Exclusion patterns (comma-separated)
**/dto/**,**/vo/**
--dry-run
Analyze only
-
Option
Short
Description
--knowledge-base
-kb
Path to existing tests for style learning
Configuration File (agent.yml)
Create in project root, JAR directory, or ~/.utagent/:
# =============================================================================
# LLM Settings
# =============================================================================
llm :
protocol : " openai" # openai | openai-zhipu | anthropic | gemini
api-key : " ${env:UT_AGENT_API_KEY}" # Supports environment variables
base-url : " ${env:UT_AGENT_BASE_URL}" # Optional, protocol default used
model-name : " gpt-4o" # Model name
temperature : 0.0 # 0.0 (precise) ~ 1.0 (creative)
timeout : 120 # Request timeout (seconds)
custom-headers : {} # Custom HTTP headers
# =============================================================================
# Workflow Settings
# =============================================================================
workflow :
max-retries : 5 # Max retry on failure
coverage-threshold : 80 # Target coverage (%)
interactive : false # Confirm before writes
use-lsp : false # Enable LSP syntax check
iterative-mode : false # Per-method test generation
method-coverage-threshold : 80 # Per-method coverage threshold
skip-low-priority : false # Skip getters/setters
max-stale-iterations : 3 # Stop after N iterations without progress
min-coverage-gain : 1 # Min coverage gain (%) per iteration
# =============================================================================
# Batch Mode Settings
# =============================================================================
batch :
exclude-patterns : " " # Glob patterns to exclude
dry-run : false # Analyze only
# =============================================================================
# Incremental Mode Settings
# =============================================================================
incremental :
mode : " uncommitted" # uncommitted | staged | compare
target-ref : " HEAD" # Target Git ref
# =============================================================================
# Recommended Dependencies (for environment check)
# =============================================================================
dependencies :
junit-jupiter : " 5.10.1"
mockito-core : " 5.8.0"
mockito-junit-jupiter : " 5.8.0"
mockito-inline : " 5.8.0"
jacoco-maven-plugin : " 0.8.11"
Configuration Options Reference
Key
Type
Default
Description
protocol
string
"openai"
LLM provider protocol
api-key
string
-
API key (supports ${env:VAR})
base-url
string
-
API base URL
model-name
string
-
Model name
temperature
float
0.0
Sampling temperature
timeout
int
120
Request timeout (seconds)
custom-headers
map
{}
Custom HTTP headers
Workflow Settings (workflow)
Key
Type
Default
Description
max-retries
int
5
Max retry attempts
coverage-threshold
int
80
Target coverage %
interactive
bool
false
Confirm before writes
use-lsp
bool
false
Enable LSP syntax checking
iterative-mode
bool
false
Per-method generation
method-coverage-threshold
int
80
Per-method coverage %
skip-low-priority
bool
false
Skip getters/setters
max-stale-iterations
int
3
Stop after N no-progress iterations
min-coverage-gain
int
1
Min coverage gain % per iteration
Key
Type
Default
Description
exclude-patterns
string
""
Comma-separated glob patterns
dry-run
bool
false
Analyze only mode
Incremental Settings (incremental)
Key
Type
Default
Description
mode
string
"uncommitted"
uncommitted / staged / compare
base-ref
string
-
Base Git ref (for compare mode)
target-ref
string
"HEAD"
Target Git ref
Variable
Description
UT_AGENT_API_KEY
LLM API key
UT_AGENT_BASE_URL
LLM API base URL
UT_AGENT_MODEL_NAME
Model name
Apache License 2.0 - See LICENSE
Made with ❤️ for Java developers