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.
- 🤖 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
~/.aimockerdirectory 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.)
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.
For other build tools (Gradle, Gradle Kotlin DSL, SBT, Leiningen), please refer to JitPack documentation
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: 2000application.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=2000Configuration Priority:
application.properties>application.yml>application.yaml
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);
}
}@Test
void testParameterInjection(
@AiMock(value = "Generate random user data with various countries", cache = true)
User user) {
assertNotNull(user);
System.out.println("Generated user: " + user);
}@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());
}- Target: Class level
- Purpose: Enables AiMocker JUnit extension for the test class
- 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)
- Initialization: AiMocker reads configuration from
application.yml/properties - Annotation Processing: Detects
@AiMockannotations on fields and parameters - Cache Check: If caching is enabled, checks local cache first
- AI Generation: Calls AI API with the provided prompt and target class structure
- Async Processing: Uses CompletableFuture for parallel API calls
- Data Injection: Deserializes JSON response and injects into test variables
- Cache Storage: Saves generated data to
~/.aimocker/using hash-based keys
-
Use Descriptive Prompts: Provide clear instructions for better data quality
@AiMock("Generate a user with age between 20-60, from Asian countries")
-
Enable Caching for Repeated Tests: Reduce API calls and token usage
@AiMock(value = "...", cache = true)
-
Specify Variety in Prompts: Get diverse test data
@AiMock("Generate users from various countries with different occupations")
-
Appropriate Count Values: Balance between test coverage and performance
@AiMock(value = "...", count = 10) // For boundary testing
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.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- GitHub: @GladerJ
- Issues: GitHub Issues