Jmockdata 1.x,2.x版本使用介绍,3.0.0版本变化较大,请参考另外一篇文章
Jmockdata
Jmockdta是一款实现模拟JAVA类型或对象的实例化并随机初始化对象的数据的工具框架。单元测试的利器。
The plug-in of Jmockdata what through random algorithm mock java data.
Jmockdata插件通过随机算法模拟Java数据.
具体介绍和用法可以参考如下:
- 模拟数据入口类 JMockData
- 被模拟的数据最好是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等结构的模拟。
4. 可以通过JmockDataContext 获取模拟过程的所有上下文信息,甚至可以打印整个模拟类型树
5. 可以通过JMockDataManager 注册模拟数据类型,重写模拟数据算法,注册模拟数据类拦截器等
6. 可以通过实现MockData来实现一个模拟数据类型
7. 可以通过实现JmockDataTemplate,或者继承JmockDataTemplateDefault来重写模拟数据算法
8. 可以通过实现JmockDataInterceptor来实现一个拦截器
9. 具体示例实践请参考https://github.com/jsonzou/jmockdata-demo
10.JDK1.7+
11. 有问题或者对本框架有新的想法,请加QQ群交流:630287196
下载引入:Download
Jmockdata-2.4
Jar
Maven
<dependency> <groupId>com.github.jsonzou</groupId> <artifactId>jmockdata</artifactId> <version>2.4</version> </dependency> ``` ## Gradle > ``` compile group: 'com.github.jsonzou', name: 'jmockdata', version: '2.4' ```
Jmockdata-2.3
Jar
Maven
<dependency> <groupId>com.github.jsonzou</groupId> <artifactId>jmockdata</artifactId> <version>2.3</version> </dependency> ``` ## Gradle > ``` compile group: 'com.github.jsonzou', name: 'jmockdata', version: '2.3' ```
Jmockdata-2.2
Jar
Maven
<dependency> <groupId>com.github.jsonzou</groupId> <artifactId>jmockdata</artifactId> <version>2.2</version> </dependency> ``` ## Gradle > ``` compile group: 'com.github.jsonzou', name: 'jmockdata', version: '2.2' ```
Jmockdata-2.0
Jar
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
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 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 {
/**
* 测试模拟简单POJO - User
*/
@Test
public void mockTest_mockSimpleType_mockPOJO() {
User user=JMockData.mockSimpleType(User.class);
print(user);
}
/**
* 测试模拟any
*/
@Test
public void mockTest_mockSimpleType_mockAny() {
MockDemoBeanAnyData anyData=JMockData.mockSimpleType(MockDemoBeanAnyData.class);
print(anyData);
}
/**
* 测试模拟元数据类型
*/
@Test
public void mockTest_mockSimpleType_mockMeta() {
System.out.println("BigIntegerData => " + JMockData.mockSimpleType(BigInteger.class));
System.out.println("BigIntegerArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(BigInteger[].class), true));
System.out.println("BigDecimalData => " + JMockData.mockSimpleType(BigDecimal.class));
System.out.println("BigDecimalArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(BigDecimal[].class), true));
System.out.println("StringData => " + JMockData.mockSimpleType(String.class));
System.out.println("StringArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(String[].class), true));
System.out.println("DateData => " + JMockData.mockSimpleType(Date.class));
System.out.println("DateArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Date[].class), true));
System.out.println("IntegerData => " +JMockData.mockSimpleType(Integer.class));
System.out.println("IntegerBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Integer[].class), true));
System.out.println("IntegerUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(int[].class), true));
System.out.println("ByteData => " + JMockData.mockSimpleType(Byte.class));
System.out.println("ByteBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Byte[].class), true));
System.out.println("ByteUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(byte[].class), true));
System.out.println("CharacterData => " + JMockData.mockSimpleType(Character.class));
System.out.println("CharacterBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Character[].class), true));
System.out.println("CharacterUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(char[].class), true));
System.out.println("BooleanData => " + JMockData.mockSimpleType(Boolean.class));
System.out.println("BooleanBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Boolean[].class), true));
System.out.println("BooleanUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(boolean[].class), true));
System.out.println("LongData => " + JMockData.mockSimpleType(Long.class));
System.out.println("LongBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Long[].class), true));
System.out.println("LongUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(long[].class), true));
System.out.println("FloatData => " + JMockData.mockSimpleType(Float.class));
System.out.println("FloatBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Float[].class), true));
System.out.println("FloatUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(float[].class), true));
System.out.println("ShortData => " + JMockData.mockSimpleType(Short.class));
System.out.println("ShortBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Short[].class), true));
System.out.println("ShortUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(short[].class), true));
System.out.println("DoubleData => " + JMockData.mockSimpleType(Double.class));
System.out.println("DoubleBoxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(Double[].class), true));
System.out.println("DoubleUnboxingArrayData => " + JSON.toJSONString(JMockData.mockSimpleType(double[].class), true));
}
/**
* 测试模拟
* List,Set,Map三种常见数据结构
*/
@Test
public void mockTest_mockCollection() {
List<String> listStr=JMockData.mockCollection(new TypeReference<List<String>>(){});
List<User[]> listUser=JMockData.mockCollection(new TypeReference<List<User[]>>(){});
Set<String> setStr=JMockData.mockCollection(new TypeReference<Set<String>>(){});
Map<String,String> mapStr=JMockData.mockCollection(new TypeReference<Map<String,String>>(){});
Map<String,User> mapUser=JMockData.mockCollection(new TypeReference<Map<String,User>>(){});
print(listStr);
print(listUser);
print(setStr);
print(mapStr);
print(mapUser);
}
/**
* 测试一次模拟全部元数据类型JmockdataWrapperMetaDataAll
* 元数据类型包括40种
* 元数据类型不需提供Wrapper类,直接调用JMockData.mockMetaDataAll().getXxxData()即可获取元数据类型模拟数据
*/
@Test
public void mockTest_mockMetaDataAll() {
JmockdataWrapperMetaDataAll metaDataMocker=JMockData.mockMetaDataAll();
System.out.println("BigIntegerData => " + metaDataMocker.getBigIntegerData());
System.out.println("BigIntegerArrayData => " + JSON.toJSONString(metaDataMocker.getBigIntegerArrayData(), true));
System.out.println("BigDecimalData => " + metaDataMocker.getBigDecimalData());
System.out.println("BigDecimalArrayData => " + JSON.toJSONString(metaDataMocker.getBigDecimalArrayData(), true));
System.out.println("StringData => " + metaDataMocker.getStringData());
System.out.println("StringArrayData => " + JSON.toJSONString(metaDataMocker.getStringArrayData(), true));
System.out.println("DateData => " + metaDataMocker.getDateData());
System.out.println("DateArrayData => " + JSON.toJSONString(metaDataMocker.getDateArrayData(), true));
System.out.println("IntegerData => " + metaDataMocker.getIntegerData());
System.out.println("IntegerBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getIntegerBoxingArrayData(), true));
System.out.println("IntegerUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getIntegerUnboxingArrayData(), true));
System.out.println("ByteData => " + metaDataMocker.getByteData());
System.out.println("ByteBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getByteBoxingArrayData(), true));
System.out.println("ByteUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getByteUnboxingArrayData(), true));
System.out.println("CharacterData => " + metaDataMocker.getCharacterData());
System.out.println("CharacterBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getCharacterBoxingArrayData(), true));
System.out.println("CharacterUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getCharacterUnboxingArrayData(), true));
System.out.println("BooleanData => " + metaDataMocker.getBooleanData());
System.out.println("BooleanBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getBooleanBoxingArrayData(), true));
System.out.println("BooleanUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getBooleanUnboxingArrayData(), true));
System.out.println("LongData => " + metaDataMocker.getLongData());
System.out.println("LongBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getLongBoxingArrayData(), true));
System.out.println("LongUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getLongUnboxingArrayData(), true));
System.out.println("FloatData => " + metaDataMocker.getFloatData());
System.out.println("FloatBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getFloatBoxingArrayData(), true));
System.out.println("FloatUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getFloatUnboxingArrayData(), true));
System.out.println("ShortData => " + metaDataMocker.getShortData());
System.out.println("ShortBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getShortBoxingArrayData(), true));
System.out.println("ShortUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getShortUnboxingArrayData(), true));
System.out.println("DoubleData => " + metaDataMocker.getDoubleData());
System.out.println("DoubleBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getDoubleBoxingArrayData(), true));
System.out.println("DoubleUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getDoubleUnboxingArrayData(), true));
}
/**
* 测试模拟单个元数据类型模拟JmockdataWrapperMetaDataSingle
* 元数据类型包括40种
* 元数据类型不需提供Wrapper类,直接调用JMockData.mockMetaDataSingle().getXxxData()即可获取元数据类型模拟数据
*/
@Test
public void mockTest_mockMetaDataSingle() {
JmockdataWrapperMetaDataSingle metaDataMocker=JMockData.mockMetaDataSingle();
System.out.println("BigIntegerData => " + metaDataMocker.getBigIntegerData());
System.out.println("BigIntegerArrayData => " + JSON.toJSONString(metaDataMocker.getBigIntegerArrayData(), true));
System.out.println("BigDecimalData => " + metaDataMocker.getBigDecimalData());
System.out.println("BigDecimalArrayData => " + JSON.toJSONString(metaDataMocker.getBigDecimalArrayData(), true));
System.out.println("StringData => " + metaDataMocker.getStringData());
System.out.println("StringArrayData => " + JSON.toJSONString(metaDataMocker.getStringArrayData(), true)); System.out.println("DateData => " + metaDataMocker.getDateData()); System.out.println("DateArrayData => " + JSON.toJSONString(metaDataMocker.getDateArrayData(), true)); System.out.println("IntegerData => " + metaDataMocker.getIntegerData()); System.out.println("IntegerBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getIntegerBoxingArrayData(), true)); System.out.println("IntegerUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getIntegerUnboxingArrayData(), true)); System.out.println("ByteData => " + metaDataMocker.getByteData()); System.out.println("ByteBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getByteBoxingArrayData(), true)); System.out.println("ByteUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getByteUnboxingArrayData(), true)); System.out.println("CharacterData => " + metaDataMocker.getCharacterData()); System.out.println("CharacterBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getCharacterBoxingArrayData(), true)); System.out.println("CharacterUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getCharacterUnboxingArrayData(), true)); System.out.println("BooleanData => " + metaDataMocker.getBooleanData()); System.out.println("BooleanBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getBooleanBoxingArrayData(), true)); System.out.println("BooleanUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getBooleanUnboxingArrayData(), true)); System.out.println("LongData => " + metaDataMocker.getLongData()); System.out.println("LongBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getLongBoxingArrayData(), true)); System.out.println("LongUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getLongUnboxingArrayData(), true)); System.out.println("FloatData => " + metaDataMocker.getFloatData()); System.out.println("FloatBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getFloatBoxingArrayData(), true)); System.out.println("FloatUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getFloatUnboxingArrayData(), true)); System.out.println("ShortData => " + metaDataMocker.getShortData()); System.out.println("ShortBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getShortBoxingArrayData(), true)); System.out.println("ShortUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getShortUnboxingArrayData(), true)); System.out.println("DoubleData => " + metaDataMocker.getDoubleData()); System.out.println("DoubleBoxingArrayData => " + JSON.toJSONString(metaDataMocker.getDoubleBoxingArrayData(), true)); System.out.println("DoubleUnboxingArrayData => " + JSON.toJSONString(metaDataMocker.getDoubleUnboxingArrayData(), true)); }
/**
* 测试模拟简单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 说明与使用
is print context info
printContext=false
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](https://www.oschina.net/p/jmockdata)
************
多谢 [@春天里的一朵花](https://my.oschina.net/u/3247151) 打赏2 元
************
### 还有要打赏小二,跪请您往下看↓↓↓↓↓↓↓↓↓↓↓↓
************