文档

快速开始

将 JMock 接入 Java 项目,几分钟内即可渲染首条模板。

1

安装

在 Maven 的 pom.xml 中加入核心依赖;若需全部内置函数,请在 runtime 引入聚合插件 all-plugin。

pom.xml
<dependency>
  <groupId>cloud.xcan.jmock</groupId>
  <artifactId>xcan-jmock.core</artifactId>
  <version>2.0.0</version>
</dependency>

<!-- Include all plugins -->
<dependency>
  <groupId>cloud.xcan.jmock</groupId>
  <artifactId>xcan-jmock.all-plugin</artifactId>
  <version>2.0.0</version>
  <scope>runtime</scope>
</dependency>
2

基本用法

示例代码
import cloud.xcan.jmock.core.engine.MockEngine;

// Simple template rendering
MockEngine engine = MockEngine.defaultEngine();
String result = engine.render("Hello @Name(), your ID is @Integer(1000,9999)");
// → "Hello Alice, your ID is 4527"

// Batch generation
List<String> records = engine.renderBatch(template, 1000);

// Single expression evaluation
Object value = engine.evaluate("@Email()");
// → "john.doe@example.com"
3

模板语法

JMock 模板使用 @函数名(参数) 形式,常见示例见下表:

表达式说明
@String()随机 6 位字符串
@Integer(1,100)1~100 之间的随机整数
@Email()随机邮箱
@Name()随机人名
@Repeat(@Email(),3)3 个随机邮箱组成的数组
4

插件机制

JMock 通过 Java SPI 发现插件,运行时自动注册全部函数。实现 MockFunction 并在 META-INF/services 中注册即可扩展自定义函数。