开源中国

我们不支持 IE 10 及以下版本浏览器

It appears you’re using an unsupported browser

为了获得更好的浏览体验,我们强烈建议您使用较新版本的 Chrome、 Firefox、 Safari 等,或者升级到最新版本的IE浏览器。 如果您使用的是 IE 11 或以上版本,请关闭“兼容性视图”。
Jmockdata首页、文档和下载 - 随机模拟 java 数据插件 - 开源中国社区
Float_left Icon_close
全部项目分类
Apache
Java 查看源码»
跨平台
分享
收藏
250 人收藏
收录时间:2017-01-05
Jmockdata 详细介绍

Jmockdata

Jmockdta是一款实现模拟JAVA类型或对象的实例化并随机初始化对象的数据的工具框架。单元测试的利器。

mark

The plugin of Jmockdata what through random algorithm mock java data.
Jmockdata插件通过随机算法模拟Java数据.

mark

Feature

  • 支持丰富多样的数据类型模拟,包括:Java基本类型、字符串、枚举、日期、数组、多维数组、集合[List|Set|Map]、Java对象等
  • 支持泛型
  • 支持继承
  • 支持循环依赖、自依赖
  • 支持改变mockConfig来自定义模拟数据策略
  • 支持JDK1.7+,无任何第三方依赖

Download

Jmockdata-3.1.0

Jar

Jmockdata-3.1.0

Maven

 

     com.github.jsonzou

     jmockdata

     3.1.0

 

Gradle

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

 

Usage

基础类型

支持以下基础类型直接模拟

描述 类型
基础类型 byte boolean char short int long float double
包装类型包装 Byte Boolean Character Short Integer Long Float Double
基础类型数组 byte[] boolean[] char[] short[] int[] long[] float[] double[]
包装类型数组 Byte[] Boolean[] Character[] Short[] Integer[] Long[] Float[] Double[]
常用类型 BigDecimal BigInteger Date String
常用类型数组 BigDecimal[] BigInteger[] Date[] String[]
二维数组 以上所有基础类型的二维数组
//基本类型模拟
int intNum = JMockData.mock(int.class);
int[] intArray = JMockData.mock(int[].class);
Integer integer = JMockData.mock(Integer.class);
Integer[] integerArray = JMockData.mock(Integer[].class);
//常用类型模拟
BigDecimal bigDecimal = JMockData.mock(BigDecimal.class);
BigInteger bigInteger = JMockData.mock(BigInteger.class);
Date date = JMockData.mock(Date.class);
String str = JMockData.mock(String.class);

JAVA对象

模拟bean,被模拟的数据必须是plain bean,底层采用了java自带的内省方法来获取字段和对应的setter方法。

支持模拟继承而来的属性。

//模拟Java对象
public class BasicBean {
  //基本类型
  private byte byteNum;
  private boolean booleanNum;
  private char charNum;
  private short shortNum;
  private int integerNum;
  private long longNum;
  private float floatNum;
  private double doubleNum;
  //基本包装类型
  private Byte byteBoxing;
  private Boolean booleanBoxing;
  private Character charBoxing;
  private Short shortBoxing;
  private Integer integerBoxing;
  private Long longBoxing;
  private Float floatBoxing;
  private Double doubleBoxing;
  //基本类型数组
  private byte[] byteNumArray;
  private boolean[] booleanNumArray;
  private char[] charNumArray;
  private short[] shortNumArray;
  private int[] integerNumArray;
  private long[] longNumArray;
  private float[] floatNumArray;
  private double[] doubleNumArray;
  //基本类型二维数组
  private byte[][] byteNumDoubleArray;
  private boolean[][] booleanNumDoubleArray;
  private char[][] charNumDoubleArray;
  private short[][] shortNumDoubleArray;
  private int[][] integerNumDoubleArray;
  private long[][] longNumDoubleArray;
  private float[][] floatNumDoubleArray;
  private double[][] doubleNumDoubleArray;
  //基本包装类型数组
  private Byte[] byteBoxingArray;
  private Boolean[] booleanBoxingArray;
  private Character[] charBoxingArray;
  private Short[] shortBoxingArray;
  private Integer[] integerBoxingArray;
  private Long[] longBoxingArray;
  private Float[] floatBoxingArray;
  private Double[] doubleBoxingArray;
  //基本包装类型二维数组
  private Byte[][] byteBoxingDoubleArray;
  private Boolean[][] booleanBoxingDoubleArray;
  private Character[][] charBoxingDoubleArray;
  private Short[][] shortBoxingDoubleArray;
  private Integer[][] integerBoxingDoubleArray;
  private Long[][] longBoxingDoubleArray;
  private Float[][] floatBoxingDoubleArray;
  private Double[][] doubleBoxingDoubleArray;
  //其他常用类型
  private BigDecimal bigDecimal;
  private BigInteger bigInteger;
  private Date date;
  private String string;
  //其他常用类型数组
  private BigDecimal[] bigDecimalArray;
  private BigInteger[] bigIntegerArray;
  private Date[] dateArray;
  private String[] stringArray;
  //其他常用类型二维数组
  private BigDecimal[][] bigDecimalDoubleArray;
  private BigInteger[][] bigIntegerDoubleArray;
  private Date[][] dateDoubleArray;
  private String[][] stringDoubleArray;
  //集合、MAP数组
  private List[] listArray;
  private Set[] setArray;
  private Map[] mapArray;
  //集合、MAP二维数组
  private List[][] listDoubleArray;
  private Set[][] setDoubleArray;
  private Map[][] mapDoubleArray;
  //集合、MAP二维数组(内部数组)
  private List[][] listInnerArrayDoubleArray;
  private Set[][] setInnerArrayDoubleArray;
  private Map[][] mapInnerArrayDoubleArray;
  //集合、MAP二维数组(内部二维数组)
  private List[][] listInnerDoubleArrayDoubleArray;
  private Set[][] setInnerDoubleArrayDoubleArray;
  private Map[][] mapInnerDoubleArrayDoubleArray;
  //LIST
  private List byteBoxingList;
  private List booleanBoxingList;
  private List charBoxingList;
  private List shortBoxingList;
  private List integerBoxingList;
  private List longBoxingList;
  private List floatBoxingList;
  private List doubleBoxingList;
  private List bigDecimalList;
  private List bigIntegerList;
  private List dateList;
  private List stringList;
  private List> stringListList;
  private List> stringSetList;
  private List> mapList;
  //数组LIST
  private List byteBoxingArrayList;
  private List booleanBoxingArrayList;
  private List charBoxingArrayList;
  private List shortBoxingArrayList;
  private List integerBoxingArrayList;
  private List longBoxingArrayList;
  private List floatBoxingArrayList;
  private List doubleBoxingArrayList;
  private List bigDecimalArrayList;
  private List bigIntegerArrayList;
  private List dateArrayList;
  private List stringArrayList;
  //二维数组LIST
  private List byteBoxingDoubleArrayList;
  private List booleanBoxingDoubleArrayList;
  private List charBoxingDoubleArrayList;
  private List shortBoxingDoubleArrayList;
  private List integerBoxingDoubleArrayList;
  private List longBoxingDoubleArrayList;
  private List floatBoxingDoubleArrayList;
  private List doubleBoxingDoubleArrayList;
  private List bigDecimalDoubleArrayList;
  private List bigIntegerDoubleArrayList;
  private List dateDoubleArrayList;
  private List stringDoubleArrayList;
  //SET忽略同List
  //MAP
  private Map basicMap;
  private Map keyArrayMap;
  private Map valueArrayMap;
  private Map keyValueArrayMap;
  private Map keyValueDoubleArrayMap;
  private Map, Map> keyListValueMapMap;
  private Map[], Map[]> keyArrayListValueArrayMapMap;
  //getter setter省略...
}

//调用模拟数据的方法模拟Java对象
BasicBean basicBean = JMockData.mock(BasicBean.class);

任意类型(LIST,SET,MAP)

@Test
//******注意TypeReference要加{}才能模拟******
public void testTypeRefrence() {
  //模拟基础类型,不建议使用这种方式,参考基础类型章节直接模拟。
  Integer integerNum = JMockData.mock(new TypeReference(){});
  Integer[] integerArray = JMockData.mock(new TypeReference(){});
  //模拟集合
  List integerList = JMockData.mock(new TypeReference>(){});
  //模拟数组集合
  List integerArrayList = JMockData.mock(new TypeReference>(){});
  //模拟集合数组
  List[] integerListArray = JMockData.mock(new TypeReference[]>(){});
  //模拟集合实体
  List basicBeanList = JMockData.mock(new TypeReference>(){});
  //各种组合忽略。。。。map同理。下面模拟一个不知道什么类型的map
  Map>, Map, Double[]>> some = JMockData.mock(new TypeReference>, Map, Double[]>>>(){});
}

更改随机范围

MockConfig mockConfig = new MockConfig()
    .byteRange((byte) 0, Byte.MAX_VALUE)
    .shortRange((short) 0, Short.MAX_VALUE)
    .intRange(0, Integer.MAX_VALUE)
    .floatRange(0.0f, Float.MAX_EXPONENT)
    .doubleRange(0.0, Double.MAX_VALUE)
    .longRange(0, Long.MAX_VALUE)
    .dateRange("2010-01-01", "2020-12-30")
    .sizeRange(5, 10)
    .stringSeed("a", "b", "c")
    .charSeed((char) 97, (char) 98);
BasicBean basicBean = JMockData.mock(BasicBean.class, mockConfig);

高级特性

循环依赖

public class AXB {
  private BXA BXA;
  private String name;
  //getter setter省略...
}
public class BXA {
  private AXB AXB;
  private String name;
  //getter setter省略...
}
@Test
public void testCircular() {
   AXB axb = JMockData.mock(AXB.class);
   AXB circularAxb = axb.getBXA().getAXB();
   assertSame(axb, circularAxb);
}

自依赖

public class SelfRefData {

  private Integer id;
  private String name;
  private SelfRefData parent;
  private Map parentMap;
  private SelfRefData[] parentArray;
  private List list;
  private List listArray;
  private List> listListArray;
  private List[] arrayList;

  private SelfRefData[][][] data;
  private Map mapArray;
  private Map> mapList;
  private Map> mapListArray;
  //getter setter省略...
}
@Test
public void testSelf() {
   SelfRefData selfRefData = JMockData.mock(SelfRefData.class);
   assertSame(selfRefData.getParent(), selfRefData);
}

泛型继承

//定义一个泛型父类
public class GenericData {
  private A a;
  private B b;
  private C c;
  private A[] aArray;
  private List bList;
  private Map map;
  private List[] cArray;
  private Map, List>[] d;
  //getter setter省略...
}

@Test
public void testGenericData() {
    GenericData genericData = JMockData.mock(new TypeReference>() {});
    assertNotNull(genericData);
  }

开发者/贡献者

JsonZou,TaoYu,KanYuXia

感谢他们的大力支持,才有了Jmockdata-3.0.0全新版本!欢迎各路兄弟参与Jmockdata的迭代,让功能更完善。

交流讨论

Jmockdata 交流群:630287196


大家对 Jmockdata 的评论 (全部 20 条评论)
{{repayCom.userName}}
rwutongyu1
非常有用啊,牛人啊
寻觅一只耳朵
上一版本出了这问题,新版本会更新。
zkool
好用
春天里的一朵花
赞,收藏分享
水中竹
好用,单元测试的必备利器。
顶部