首页 > 学院 > 开发设计 > 正文

Jmockdata 使用介绍(再也不用苦逼的造测试数据了)

2019-11-10 18:03:28
字体:
来源:转载
供稿:网友

Jmockdata

Jmockdta是一款实现模拟java类型或对象的实例化并随机初始化对象的数据的工具框架。单元测试的利器。The plug-in of Jmockdata what through random algorithm mock java data.Jmockdata插件通过随机算法模拟Java数据.

具体介绍和用法可以参考如下:

模拟数据入口方法 JMockData.mock(JmockDataWrapper)被模拟数据必须继承JmockDataWrapper经过它的包装被模拟的数据最好是plain bean,只提供getter,setter,has,is方法的才可以被模拟

框架默认实现了40个元数据类型的数据模拟器包括:

short.class,Short.class,short[].class,Short[].class, int.class,Integer.class,int[].class,Integer[].class, long.class,Long.class,long[].class,Long[].class, float.class,Float.class,float[].class,Float[].class, double.class,Double.class,double[].class,Double[].class, boolean.class,Boolean.class,boolean[].class,Boolean[].class, char.class,Character.class,char[].class,Character[].class, String.class,String[].class, BigDecimal.class,BigDecimal[].class, BigInteger.class,BigInteger[].class, Date.class,Date[].class

通过对以上基本元数据类型的模拟实现,能够进一步实现BEAN,LIST,SET,ARRAY等结构的模拟。

可以通过JmockDataContext 获取模拟过程的所有上下文信息,甚至可以打印整个模拟类型树 可以通过JMockDataManager 注册模拟数据类型,重写模拟数据算法,注册模拟数据类拦截器等可以通过实现MockData来实现一个模拟数据类型可以通过实现JmockDataTemplate,或者继承JmockDataTemplateDefault来重写模拟数据算法可以通过实现JmockDataInterceptor来实现一个拦截器具体示例实践请参考https://github.com/jsonzou/jmockdata-demo有问题或者对本框架有新的想法,请联系我, 作者:jsonzou

下载引入:Download

Jmockdata-2.0

Jar

Jmockdata-2.0

Maven

<dependency> <groupId>com.github.jsonzou</groupId> <artifactId>jmockdata</artifactId> <version>2.0</version> </dependency>

Gradle

compile group: 'com.github.jsonzou', name: 'jmockdata', version: '2.0'

Jmockdata-1.0

Jar

Jmockdata-1.0

Maven

<dependency> <groupId>com.github.jsonzou</groupId> <artifactId>jmockdata</artifactId> <version>1.0</version> </dependency>

Gradle

compile group: 'com.github.jsonzou', name: 'jmockdata', version: '1.0'

测试示例

具体示例请参考:https://github.com/jsonzou/jmockdata-demo或者下载该示例项目Please refer to the specific example:https://github.com/jsonzou/jmockdata-demo Ordownload the demo PRoject>

示例代码片段如下:

/* * Copyright (C) 2016 Baidu, Inc. All Rights Reserved. */ package org.jsonzou.jmockdata.test; import com.alibaba.fastjson.JSON; import org.jsonzou.jmockdata.JMockData; import org.jsonzou.jmockdata.mockdata.JMockDataManager; import org.jsonzou.jmockdata.test.custommocktemplate.CustomJmockDataTemplate; import org.jsonzou.jmockdata.test.interceptor.OneJmockDataInterceptor; import org.jsonzou.jmockdata.test.registermockdatabean.MockDataStringBuffer; import org.jsonzou.jmockdata.test.wrapperbean.*; import org.junit.Test; /** * 测试 * * @author jsonzou * @version 1.0 * @since 2016/12/26 */ public class JMockDataTest { /** * 测试模拟简单List<String> */ @Test public void mockTest_SimpleStringList() { MockDemoSimpleListStringWrapper mockData = JMockData.mock(MockDemoSimpleListStringWrapper.class); mockData.getJmockDataContext().printTree(); // 打印类型树 print(mockData.getList()); } /** * 测试模拟任意类型的数据 */ @Test public void mockTest_MockAnyData() { MockDemoBeanAnyDataWrapper mockData = JMockData.mock(MockDemoBeanAnyDataWrapper.class); // mockData.getJmockDataContext().printTree(); // 打印类型树 mockData.setJmockDataContext(null); print(mockData); } /** * 测试模拟自循环引用的数据 */ @Test public void mockTest_MockSelfRefData() { JMockDataManager.getInstance().setMaxSelfRefLevel(4);// 设置自循环体循环层级上限 MockDemoBeanSelfRefDataWrapper mockData = JMockData.mock(MockDemoBeanSelfRefDataWrapper.class); // mockData.getJmockDataContext().printTree(); // 打印类型树 mockData.setJmockDataContext(null); print(mockData); } /** * 测试模拟类型拦截器,之拦截String类型的模拟 */ @Test public void mockTest_Interceptor() { JMockDataManager.getInstance().interceptors(OneJmockDataInterceptor.class); // 拦截器 =》只拦截String类型 MockDemoBeanSimpleDataWrapper mockData = JMockData.mock(MockDemoBeanSimpleDataWrapper.class); print(mockData.getSimpleData()); } /** * 测试模拟,增加自定义模拟类型,StringBuffer */ @Test public void mockTest_RegisterMockDataBean() { JMockDataManager.getInstance().registerMockData(new MockDataStringBuffer(), StringBuffer.class); MockDemoCustomStringBufferWrapper mockData = JMockData.mock(MockDemoCustomStringBufferWrapper.class); print(mockData.getStringBuffer()); } /** * 测试模拟,自定义模拟数据模板方法 */ @Test public void mockTest_CustomJmockDataTemplate() { JMockDataManager.getInstance().setMockTemplate(new CustomJmockDataTemplate()); MockDemoBeanSimpleDataWrapper mockData = JMockData.mock(MockDemoBeanSimpleDataWrapper.class); print(mockData.getSimpleData()); } /** * 打印json结构 * * @param data */ private void print(Object data) { System.out.println(JSON.toJSONString(data, true)); } }

配置文件config.properties 说明与使用

## value[seed,seed,seed]seedCharacter= 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y ZseedString= 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z## valueRange[min,max] / [fixed value]rangeShort=0,10rangeInteger=0,10# Double & BigdecimalrangeDouble=0.0,10.00 rangeFloat=0.0,10.00# Long & BigIntegerrangeLong=0.0,10.00## range-YMDhms[Y-min,Y-max,M-min,M-max,D-min,D-max,h-min,h-max,m-min,m-max,s-min,s-max]# default current yearrangeDateY=rangeDateM=0,12rangeDateD=0,30rangeDateH=0,60rangeDateMi=0,60rangeDateS=0,60# range-boolean [false,true] / [fixed value]rangeBoolean=false,true## arrSizeRange[arrSizeRange-min,arrSizeRange-max] / [fixed size]arrsizeBean=0,10arrsizeBigdecimal=0,10arrsizeBiginteger=0,10arrsizeBoolean=0,10arrsizeByte=0,10arrsizeCharacter=0,10arrsizeDate=0,10arrsizeDouble=0,10arrsizeFloat=0,10arrsizeInteger=0,10arrsizeLong=0,10arrsizeShort=0,10arrsizeString=0,10

代码示例

/** * 测试配置文件 》 改变默认算法行为 */ @Test public void mockTest_config() { JMockDataManager.getInstance().config("conf/config.properties"); // System.out.println(JSONObject.toJSONString(JMockDataManager.getInstance().config(),true)); MockDemoBeanConfigWrapper mockData = JMockData.mock(MockDemoBeanConfigWrapper.class); mockData.setJmockDataContext(null); print(mockData); }

开源中国>Jmockdata


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表