Skip to content

GladerJ/AiMocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AiMocker

Readme 中文 img License

Overview

AiMocker is an AI-powered test data mocking framework that integrates seamlessly with JUnit 5. It leverages AI APIs to generate realistic POJO (Plain Old Java Object) test data, significantly reducing the manual effort required for test data preparation.

✨ Key Features

  • 🤖 AI-Powered Data Generation: Automatically generates realistic test data using AI models
  • 🚀 Async Processing: Uses CompletableFuture for concurrent API calls to improve performance
  • 💾 Smart Caching: Local hash-based caching in ~/.aimocker directory to minimize token usage
  • 📦 Flexible Data Types: Supports single objects, Lists, and Arrays
  • 🔧 Easy Configuration: Simple YAML/Properties configuration
  • 🎯 JUnit 5 Integration: Seamless integration with JUnit 5 testing framework
  • 🌐 Multi-Provider Support: Compatible with OpenAI-compatible APIs (OpenAI, SiliconFlow, etc.)

📦 Installation

Maven

Add the JitPack repository and dependency to your pom.xml:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.github.GladerJ</groupId>
        <artifactId>AiMocker</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
    
    <!-- For non-Spring Boot projects, add JUnit 5 manually -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Note: Spring Boot projects already include JUnit 5, so you only need the AiMocker dependency.

Gradle, SBT, Leiningen

For other build tools (Gradle, Gradle Kotlin DSL, SBT, Leiningen), please refer to JitPack documentation

⚙️ Configuration

Create a configuration file in test/resources/:

application.yml (Recommended):

aimocker:
  llm:
    api-key: your-api-key-here
    api-url: https://api.openai.com/v1/chat/completions
    model: gpt-3.5-turbo
    temperature: 1.0
    max-tokens: 2000

application.properties:

aimocker.llm.api-key=your-api-key-here
aimocker.llm.api-url=https://api.openai.com/v1/chat/completions
aimocker.llm.model=gpt-3.5-turbo
aimocker.llm.temperature=1.0
aimocker.llm.max-tokens=2000

Configuration Priority: application.properties > application.yml > application.yaml

🚀 Quick Start

Basic Usage

import org.junit.jupiter.api.Test;
import top.mygld.aimocker.anno.AiMock;
import top.mygld.aimocker.anno.AiMockTest;

@AiMockTest
public class UserServiceTest {
    
    // Field injection
    @AiMock("Generate a mock user with realistic data")
    private User user;
    
    @AiMock("Generate a mock animal with name and species")
    private Animal animal;
    
    @Test
    void testFieldInjection() {
        System.out.println("User: " + user);
        System.out.println("Animal: " + animal);
    }
}

Parameter Injection

@Test
void testParameterInjection(
    @AiMock(value = "Generate random user data with various countries", cache = true) 
    User user) {
    
    assertNotNull(user);
    System.out.println("Generated user: " + user);
}

List/Array Support

@Test
void testListGeneration(
    @AiMock(value = "Generate diverse user data from different countries", 
            count = 5, 
            cache = true) 
    List<User> users) {
    
    assertEquals(5, users.size());
    users.forEach(System.out::println);
}

@Test
void testArrayGeneration(
    @AiMock(value = "Generate random international users", count = 10) 
    ArrayList<User> users) {
    
    assertEquals(10, users.size());
}

📝 Annotation Reference

@AiMockTest

  • Target: Class level
  • Purpose: Enables AiMocker JUnit extension for the test class

@AiMock

  • Target: Field or Parameter
  • Attributes
    • value: Prompt description for AI data generation (required)
    • count: Number of items to generate for collections (default: 1)
    • cache: Enable local caching (default: false)

🔄 How It Works

  1. Initialization: AiMocker reads configuration from application.yml/properties
  2. Annotation Processing: Detects @AiMock annotations on fields and parameters
  3. Cache Check: If caching is enabled, checks local cache first
  4. AI Generation: Calls AI API with the provided prompt and target class structure
  5. Async Processing: Uses CompletableFuture for parallel API calls
  6. Data Injection: Deserializes JSON response and injects into test variables
  7. Cache Storage: Saves generated data to ~/.aimocker/ using hash-based keys

🌟 Best Practices

  1. Use Descriptive Prompts: Provide clear instructions for better data quality

    @AiMock("Generate a user with age between 20-60, from Asian countries")
  2. Enable Caching for Repeated Tests: Reduce API calls and token usage

    @AiMock(value = "...", cache = true)
  3. Specify Variety in Prompts: Get diverse test data

    @AiMock("Generate users from various countries with different occupations")
  4. Appropriate Count Values: Balance between test coverage and performance

    @AiMock(value = "...", count = 10) // For boundary testing

🛠️ Supported AI Providers

AiMocker supports any OpenAI-compatible API:

  • OpenAI (GPT-3.5, GPT-4)
  • Azure OpenAI
  • SiliconFlow
  • Anthropic Claude (with compatible proxy)
  • Local models via LM Studio, Ollama, etc.

📄 License

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

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📧 Contact

About

基于 Ai 的轻量 Mock 框架

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages