Jmockdata 使用介绍(再也不用苦逼的造测试数据了)
2017-02-08 18:13
1836 查看
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.0Maven
<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.0Maven
<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 Or
download 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 Z seedString= 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,10 rangeInteger=0,10 # Double & Bigdecimal rangeDouble=0.0,10.00 rangeFloat=0.0,10.00 # Long & BigInteger rangeLong=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 year rangeDateY= rangeDateM=0,12 rangeDateD=0,30 rangeDateH=0,60 rangeDateMi=0,60 rangeDateS=0,60 # range-boolean [false,true] / [fixed value] rangeBoolean=false,true ## arrSizeRange[arrSizeRange-min,arrSizeRange-max] / [fixed size] arrsizeBean=0,10 arrsizeBigdecimal=0,10 arrsizeBiginteger=0,10 arrsizeBoolean=0,10 arrsizeByte=0,10 arrsizeCharacter=0,10 arrsizeDate=0,10 arrsizeDouble=0,10 arrsizeFloat=0,10 arrsizeInteger=0,10 arrsizeLong=0,10 arrsizeShort=0,10 arrsizeString=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
相关文章推荐
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- ios数据持久化--CoreData框架的介绍和使用
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 介绍一款chrom浏览器插件 DHC是一款使用chrome模拟REST客户端向服务器发送测试数据的谷歌浏览器插件
- 数据绑定之DataFormatString使用介绍
- WPF 数据模板DataType属性的使用,不用指定ItemTemplate
- eclipse中使用真机(已root)测试android程序时,看不到程序包data文件夹下的数据
- 数据绑定之DataFormatString使用介绍
- Python数据增强(data augmentation)库--Augmentor 使用介绍
- 数据绑定之DataFormatString使用介绍
- Lesson 04: Reading Data with the SqlDataReader(使用SqlDataReader读取数据)
- 如何在 Web 服务中使用 Service Data Objects 2.1 的动态数据 API
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定之二:使用外部URL的XML文件)
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
- 使用Hibernate处理数据(Hibernate Your Data)(英语原文)
- Data Access Application Block(Enterprise Library 3.1)的下载以及使用方法介绍
- 使用SqlDataReader读取数据示例
- 使用SqlDataReader读取数据示例
- Lesson 05: Working with Disconnected Data - The DataSet and SqlDataAdapter(使用断开数据--DataSet和SqlDataAdapter)
- 如何使用 DataBinder.Eval(Container.DataItem,"num","{0:p}") 对DataList 进行数据绑定