Skip to content

AI-powered tool that automatically generates comprehensive JUnit test suites from Java source code using Claude 3 via AWS Bedrock

License

Notifications You must be signed in to change notification settings

rtananthan/java-test-writer-assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– Java Test Writer Assistant

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.

Python AWS Bedrock JUnit 5 Claude 3

πŸš€ Key Capabilities

Intelligent Test Generation

  • 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

Advanced AI Integration

  • 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

Enterprise-Ready Architecture

  • 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

πŸ—οΈ Architecture

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       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Agent Responsibilities

  1. JavaParser Agent: Extracts structural information from Java source code
  2. TestStrategy Agent: Determines optimal testing approaches and scenarios
  3. TestWriter Agent: Generates actual JUnit test implementations using Claude AI

πŸ“‹ Prerequisites

  • Python 3.11+
  • AWS Account with Bedrock access
  • Claude 3 Haiku model enabled in AWS Bedrock
  • AWS CLI configured with appropriate credentials

πŸ› οΈ Setup Instructions

1. Clone the Repository

git clone https://github.com/your-username/java-test-writer-assistant.git
cd java-test-writer-assistant

2. Install Dependencies

pip install -r requirements.txt

3. Configure AWS Credentials

# 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

4. Enable Claude 3 in AWS Bedrock

  1. Navigate to AWS Bedrock Console
  2. Go to Model access in the left sidebar
  3. Click Manage model access
  4. Find Anthropic section and enable Claude 3 Haiku
  5. Submit the access request (usually approved immediately)

5. Configure the Application

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

🎯 Usage

Basic Usage

Generate tests for a Java file:

python runner.py sample_inputs/Calculator.java

Advanced Options

# 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

Example Output

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));
}

πŸ“ Project Structure

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

βš™οΈ Configuration Options

AWS Bedrock Settings

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

Alternative Claude Models

  • 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.

🎨 Generated Test Features

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

πŸ”§ Customization

Adding Custom Test Strategies

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

Modifying AI Prompts

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

πŸ“Š Performance & Cost

Typical Performance

  • 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)

Optimization Tips

  • 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

πŸš€ Future Enhancements

  • 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

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™‹β€β™‚οΈ Support

πŸ† Acknowledgments

  • 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.

About

AI-powered tool that automatically generates comprehensive JUnit test suites from Java source code using Claude 3 via AWS Bedrock

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published