JMock is a high-performance data generation and simulation library implemented in Java. It generates data that more closely resembles real business data characteristics compared to random data generation.
- High-performance data generation:
Generates 2M+ user records per second (single thread). Each record contains 10+ attributes totaling 200+ bytes. - Business-realistic data with i18n support:
Produces data that accurately simulates real-world business scenarios with internationalization capabilities. - Dual definition approaches:
- Annotation-based: Applied to class fields
- Function-based: Used in script files (txt, json, yml, etc.)
- Multiple storage options:
Supports JDBC, in-memory, and local filesystem persistence for batch data. - Extensible architecture:
Plugin-based system for custom mock data functions.
Type | Parameterless Form | Parameterized Form |
---|---|---|
Annotation | @Annotation |
@Annotation(param1=value1, param2=value2) |
Function | @Function() |
@Function(param1, param2) or @Function(param1,,param3) |
-
Core Symbols
@
- Marks expression start()
- Encloses parameters,
- Separates parameters|
- Delimits array elements (array parameters only)\
- Escapes special characters (@
,,
,|
)
-
Naming Conventions
- Annotation/Function Names: UpperCamelCase
Example:EmailValidator
,RandomNumber
- Parameter Names: lowerCamelCase
Example:minValue
,maxLength
- Annotation/Function Names: UpperCamelCase
-
Parameter Rules
- Parameterless calls: Omit parentheses (annotations) or use empty
()
(functions) - Parameterized calls:
- Annotation:
param=value
key-value pairs - Function: Positional values
- Annotation:
- Omitted parameters: Preserve commas
Example:@Generate(1,,3)
indicates empty second parameter
- Parameterless calls: Omit parentheses (annotations) or use empty
-
Special Character Handling
Escape@
,,
,|
in parameter values:
Example:@String("tom\@domain.com\|nick\@domain.com")
- Add Maven Dependency
<dependency>
<groupId>cloud.xcan.jmock</groupId>
<artifactId>xcan-jmock.core</artifactId>
<version>1.0.0</version>
</dependency>
- Generate Sample Data
// Define template
String content = """
{
"name": "@Name()",
"email": "@Email()",
"phone": "@Mobile()",
"address": "@Address()",
"hobbies": [ "reading", "hiking", "cooking" ]
}""";
// Process mock functions
String result = new DefaultMockTextReplacer().replace(content);
// Output result
System.out.println(result);
Output:
{
"name": "Durfee Jacob",
"email": "9alJWYsUGJuJZtGuXT@yahoo.com.cn",
"phone": "15292153757",
"address": "ul. Akademika Pavlova, 12к3, Moskva",
"hobbies": [ "reading", "hiking", "cooking" ]
}