Jmockdata是一款实现模拟JAVA类型或对象的实例化并随机初始化对象的数据的工具框架。
第一步:添加依赖
com.github.jsonzou jmockdata 1.0
第二步:测试使用示例
package com.zhaogang.jmockdata;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import com.github.jsonzou.jmockdata.JMockData;import com.zhaogang.model.User;import org.junit.Test;/** * Created by weixiang.wu on 2017/9/13. */public class TestMockData { /** * 打印json结构 * * @param data */ private void print(Object data) { System.out.println(JSON.toJSONStringWithDateFormat(data, "YYYY-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat)); } @Test public void mockTest_mockSimpleType_mockPOJO() { User user= JMockData.mockSimpleType(User.class); print(user); }}
/* * Copyright (C) 2017 Baidu, Inc. All Rights Reserved. */package com.github.jsonzou.jmockdata.test;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import com.github.jsonzou.jmockdata.JMockData;import com.github.jsonzou.jmockdata.mockdata.JMockDataManager;import com.github.jsonzou.jmockdata.mockdata.JmockdataWrapperMetaDataAll;import com.github.jsonzou.jmockdata.mockdata.JmockdataWrapperMetaDataSingle;import com.github.jsonzou.jmockdata.test.custommocktemplate.CustomJmockDataTemplate;import com.github.jsonzou.jmockdata.test.interceptor.OneJmockDataInterceptor;import com.github.jsonzou.jmockdata.test.registermockdatabean.MockDataStringBuffer;import com.github.jsonzou.jmockdata.test.testbean.MockDemoBeanAnyData;import com.github.jsonzou.jmockdata.test.testbean.User;import com.github.jsonzou.jmockdata.test.wrapperbean.*;import com.github.jsonzou.jmockdata.utils.TypeReference;import org.junit.Test;import java.math.BigDecimal;import java.math.BigInteger;import java.util.Date;import java.util.List;import java.util.Map;import java.util.Set;/** * 测试 * * @author jsonzou(keko-boy@163.com) * @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 listStr= JMockData.mockCollection(new TypeReference >(){}); List listUser= JMockData.mockCollection(new TypeReference >(){}); Set setStr= JMockData.mockCollection(new TypeReference >(){}); Map mapStr= JMockData.mockCollection(new TypeReference