An AI-powered tool that automatically generates comprehensive JUnit test suites from Java source code using Claude 3 via AWS Bedrock. This project demonstrates advanced prompt engineering, agent-based architecture, and practical AI integration for software development workflows.
- Automated JUnit 5 test creation from Java source code
- Smart test strategies including happy path, edge cases, and exception scenarios
- Proper Arrange-Act-Assert patterns with real assertions
- Exception testing with
assertThrows()
for robust error handling - Mocking integration suggestions for complex dependencies
- Claude 3 Haiku via AWS Bedrock for fast, cost-effective test generation
- Intelligent prompt engineering with response cleaning and formatting
- Rate limiting protection with configurable delays to prevent API throttling
- Graceful fallback to templates when AI services are unavailable
- Real-time progress feedback with success/failure indicators
- Modular agent-based design following single responsibility principle
- Externalized configuration for easy deployment and customization
- Comprehensive error handling with detailed logging and recovery
- Production-quality output generating compilable Java test code
- Scalable processing suitable for large codebases
The project uses a sophisticated agent-based architecture where each component has a specific responsibility:
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Java Parser βββββΆβ Test Strategy βββββΆβ Test Writer β
β Agent β β Agent β β Agent β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Extract classes β β Analyze test β β Generate JUnit β
β methods, fields β β scenarios & β β code with β
β & dependencies β β requirements β β Claude AI β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
- JavaParser Agent: Extracts structural information from Java source code
- TestStrategy Agent: Determines optimal testing approaches and scenarios
- TestWriter Agent: Generates actual JUnit test implementations using Claude AI
- Python 3.11+
- AWS Account with Bedrock access
- Claude 3 Haiku model enabled in AWS Bedrock
- AWS CLI configured with appropriate credentials
git clone https://github.com/your-username/java-test-writer-assistant.git
cd java-test-writer-assistant
pip install -r requirements.txt
# Option 1: Using AWS CLI
aws configure
# Option 2: Using environment variables
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_DEFAULT_REGION=ap-southeast-2
- Navigate to AWS Bedrock Console
- Go to Model access in the left sidebar
- Click Manage model access
- Find Anthropic section and enable Claude 3 Haiku
- Submit the access request (usually approved immediately)
Edit config.py
to customize settings:
# AWS Bedrock Configuration
AWS_REGION = "ap-southeast-2" # Your preferred region
CLAUDE_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0"
API_DELAY_SECONDS = 10 # Adjust based on your rate limits
Generate tests for a Java file:
python runner.py sample_inputs/Calculator.java
# Specify custom output directory
python runner.py sample_inputs/Calculator.java -o my_tests/
# Enable debug mode for detailed logging
python runner.py sample_inputs/Calculator.java --debug
# Get help
python runner.py --help
For the provided Calculator.java
sample, the tool generates comprehensive tests:
@Test
void testAddHappyPath() {
// Arrange
Calculator calculator = new Calculator();
int a = 5;
int b = 3;
// Act
int result = calculator.add(a, b);
// Assert
assertEquals(8, result);
}
@Test
void testDivideWithZero() {
// Arrange
Calculator calculator = new Calculator();
int a = 10;
int b = 0;
// Act & Assert
assertThrows(ArithmeticException.class, () -> calculator.divide(a, b));
}
java-test-writer-assistant/
βββ agents/ # Core AI agents
β βββ __init__.py
β βββ java_parser.py # Java source code parsing
β βββ test_strategy.py # Test scenario generation
β βββ test_writer.py # AI-powered test code generation
βββ sample_inputs/ # Example Java files
β βββ Calculator.java # Sample calculator class
βββ outputs/ # Generated test files
β βββ CalculatorTest.java # Generated test suite
βββ config.py # Configuration settings
βββ runner.py # Main orchestration script
βββ requirements.txt # Python dependencies
βββ README.md # This file
AWS_REGION = "ap-southeast-2" # AWS region for Bedrock
CLAUDE_MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0" # Claude model
MAX_TOKENS = 1000 # Max tokens per API call
API_DELAY_SECONDS = 10 # Delay between API calls
- Claude 3 Haiku: Fast and cost-effective (default)
- Claude 3 Sonnet: Balanced performance and quality
- Claude 3.5 Sonnet: Highest quality, slower and more expensive
Update CLAUDE_MODEL_ID
in config.py
to switch models.
The AI generates tests with:
- β
Proper JUnit 5 annotations (
@Test
,@BeforeEach
) - β
Comprehensive assertions (
assertEquals
,assertTrue
,assertThrows
) - β Exception handling for error conditions
- β Edge case testing (zero values, null inputs, boundary conditions)
- β Mocking setup suggestions for complex dependencies
- β Clean code structure with proper indentation and formatting
- β Descriptive test names following naming conventions
Extend TestStrategy
agent in agents/test_strategy.py
:
def _create_custom_test_cases(self, method):
# Add your custom test case logic
return custom_test_cases
Update prompts in TestWriter
agent (agents/test_writer.py
):
def _build_test_generation_prompt(self, test_method):
# Customize the prompt for specific requirements
return enhanced_prompt
- Processing Speed: ~15 test methods in 2.5 minutes (with 10s delays)
- Success Rate: >95% with Claude 3 Haiku
- Cost: ~$0.01-0.02 per test file (varies by complexity)
- Reduce
API_DELAY_SECONDS
if you have higher rate limits - Use Claude 3 Haiku for faster, cheaper generation
- Process multiple files in parallel for large codebases
- Batch processing for multiple Java files
- Spring Boot test support with
@MockBean
and@WebMvcTest
- Database testing with TestContainers integration
- IDE plugin for IntelliJ IDEA and VS Code
- Custom test templates for specific frameworks
- Test coverage analysis and gap identification
- Integration with CI/CD pipelines
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Wiki
- Anthropic for Claude 3 AI models
- AWS Bedrock for AI model hosting and inference
- JUnit 5 team for the excellent testing framework
- Python community for robust libraries and tools
Built with β€οΈ for the developer community to accelerate test-driven development through AI automation.