Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 11, 2025. It is now read-only.
/ CatMock Public archive

Java下使用mock.js生成虚拟数据。

License

Notifications You must be signed in to change notification settings

keleus/CatMock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

35 Commits

Repository files navigation

CatMock

CatMock是一个mock.js 的Java封装库。使用JDK自带的js脚本引擎直接调用mock.js脚本,实现对mock.js的统一。让接口设计阶段产生的项目资产能被后端测试复用。

Maven

中央仓库地址:CatMock

使用Graal.js引擎

<dependency>
 <groupId>cn.myzju.mock</groupId>
 <artifactId>CatMock</artifactId>
 <version>2.0.0</version>
</dependency>

使用Nashorn引擎

不推荐,JDK11中Nashorn引擎已经废弃

<dependency>
 <groupId>cn.myzju.mock</groupId>
 <artifactId>CatMock</artifactId>
 <version>1.2.2</version>
</dependency>

Mock使用说明

获取CatMock对象

2.x版本CatMock构造方法

CatMock()
CatMock(ObjectMapper mapper)
CatMock(File file)
CatMock(File file, ObjectMapper mapper)
CatMock(URL url)
CatMock(URL url, ObjectMapper mapper)

1.x版本CatMock构造方法

//使用内置的mock.js文件初始化
CatMock catMock = new CatMock();
//使用外置的mock.js文件进行初始化
CatMock catMock = new CatMock(new FileReader("{path}/mock.js"));
//使用内置的mock.js文件初始化,并允许自定义内置的ObjectMapper
CatMock catMock = new CatMock(new ObejctMapper());
//使用外置的mock.js文件进行初始化,并允许自定义内置的ObjectMapper
CatMock catMock = new CatMock(new FileReader("{path}/mock.js"),new ObejctMapper());

getMapper()

可以获得内置的ObjectMapper对象,对其进行配置了。

catmock.getMapper().setSerializationInclusion(Include.ALWAYS); 
catmock.getMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

Random

传入参数:functionfunction('args'),返回类型为String

catMock.random("string"); //👉P^7
catMock.random("lower(\"HELLO\")");//👉hello

当前已知不兼容函数:

  • dataImage()

Mock

示例可参考mock.js官方示例

生成Java对象

传入字符串必须以{开始,并以}结束

Person person = catMock.mockObject("{\"name\":\"@string\"}",Person.class);

生成Java List

传入字符串必须以[开始,并以]结束,如果传入字符串非JsonArray格式,会返回一个size为0的List;如果传入字符串非json格式,可能会报错。

List<Person> persons = catMock.mockArray("[{\"name\":\"@string\"},{\"name\":\"@string\"}]",Person.class); 

生成String

1.X 版本中,mock方法传入非JSON格式的字符串(如下e.g.1所示)时,前后一定要加上单引号。

2.X 以上版本中,mock方法传入非JSON格式的字符串(如下e.g.3所示)时,前后不用加上单引号,加上单引号时,输出结果会加上双引号。

//e.g.1
catMock.mock("'@string'");//👉Anna Jackson
//e.g.2
catMock.mock("{\n'regexp|1-5': /\\d{5,10}\\-/\n}");//👉{"regexp": "5912165-6588485-0462848-"}
//e.g.3
catMock.mock("'@string'");//👉"Anna Jackson"
catMock.mock("@string");//👉Anna Jackson

Extend

生成CatMock对象后,可以通过extend加载自定义函数。

例如新增一个名为constellation,用于获取随机星座名称的方法,JavaScript代码如下:

{
 constellation: function(date) {
 var constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
 return this.pick(constellations)
 }
}

在CatMock中,将脚本代码直接以String类型传入extend函数:

CatMock catMock = new CatMock()
catMock.extend("{\n" +
 " constellation: function(date) {\n" +
 " var constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']\n" +
 " return this.pick(constellations)\n" + 
 " }\n" +
 "}");
catMock.random("constellation")//👉水瓶座
catMock.mock("@constellation")//👉白羊座

Container使用说明

获取CatContainer

//获取的容器内部采用HashMap存储变量
CatContainer container = CatContainer.commonContainer();
//获取的容器内部采用ConcurrentHashMap存储变量
CatContainer container = CatContainer.concurrentContainer();
//获取的容器内部采用HashMap存储变量,并允许自定义内置的ObjectMapper
CatContainer container = CatContainer.commonContainer(new ObejctMapper());
//获取的容器内部采用ConcurrentHashMap存储变量,并允许自定义内置的ObjectMapper
CatContainer container = CatContainer.concurrentContainer(new ObejctMapper());

put()

和常规Map的put有所不同,CatContainer会深入解析符合json串格式的值。

运行如下代码,会在CatContainer内置的Map中产生a1a1[0]a1[1]a1[0].dataa1[0].data.tokena1[1].dataa1[1].data.token<K, V>键值对。

container.put("a1","[{\"data\":{\"token\":\"AiOiJKV1\"}},{\"data\":{\"token\":\"J9eXCt9c\"}}]");

get()

通过key值获取内置Map中存储的对应value

container.get("a1[0].data.token")//👉AiOiJKV1

translate()

将传入字符串中的${key}替换成相应的值,支持嵌套${${}}从内至外顺序解析,如下所示。

//加载数据
container.put("a1","[{\"data\":{\"token\":\"AiOiJKV1\"}},{\"data\":{\"token\":\"J9eXCt9c\"}},{\"data\":{\"tokens\":[\"J9eXCt9c\",\"AiOiJKV1\"]}}]");
container.put("a2","data.token");
container.translate("Bearer ${a1[2].${a2}s[0]}")//👉Bearer J9eXCt9c

getParams()

获得用于存储<K, V>键值对的内置Map对象,可以通过container.getParams().put()插入不愿深入解析的json字符串。

其余函数为内置Map的封装,用法与Map相同

getMapper()

可以获得内置的ObjectMapper对象,对其进行配置了。

container.getMapper().setSerializationInclusion(Include.ALWAYS); 
container.getMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

Development Plan

  • mock.js后续版本的兼容
  • 修复JSON处理引擎为Jackson后可能存在的BUG
  • 修复js引擎修改为Graal.js后可能存在的BUG

License

CatMock is available under the terms of the MIT License.

About

Java下使用mock.js生成虚拟数据。

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

Languages

AltStyle によって変換されたページ (->オリジナル) /