领导居然让我用Java造假数据

2020-12-29 / 0评 / Java

1. 前言
领导说,明天甲方要来看产品,你得搞点数据,而且数据必须是“真”的,演示效果要好看一些,这样甲方才会采购咱们的产品。
一般开发接到这种过分要求都不会很乐意去做,这完全是体力劳动了,而且很棘手。今天土豆丝不辣就教你一招,让你做出B真的“假”数据。

2. javaFaker
点击查看原图
这张图大家都很熟悉吧。我们Java也能搞Fake,它就是JavaFaker,专门用来制作仿真数据。你只需要在项目中引入:

<dependency>
     <groupId>com.github.javafaker</groupId>
     <artifactId>javafaker</artifactId>
     <version>1.0.2</version>
</dependency>
忘记说了JavaFaker主要的使用场景就是为了制造仿真测试数据,而并不是用来造假。
然后土豆丝不辣来演示一下它的用法,先定义一个 Java Bean :
/**
 * @author houxiurong
 * @date 2020/12/29
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
public class UserInfo {
    /**
     * id
     */
    private Long userId;
    /**
     * 真实姓名
     */
    private String realName;
    /**
     * 手机
     */
    private String phone;
    /**
     * 大学
     */
    private String universityName;
    /**
     * 城市
     */
    private String city;
    /**
     * 地址
     */
    private String street;
}
然后声明一个Faker对象,你可以根据需要进行本地化声明,如果是使用中国的:
 Faker fakerWithCN = new Faker(Locale.CHINA);   
你要是想造点美帝的数据就改成这样:
 Faker fakerWithCN = new Faker(Locale.US);
这里我们使用中国来生成模拟 20 条仿真数据:
public static void main(String[] args) {
    Faker fakerWithCN = new Faker(Locale.CHINA);
    CopyOnWriteArrayList copyOnWriteList = new CopyOnWriteArrayList();
    for (int i = 0; i < 20; i++) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(fakerWithCN.number().randomNumber() + i);
        userInfo.setRealName(fakerWithCN.name().fullName());
        userInfo.setPhone(fakerWithCN.phoneNumber().cellPhone());
        userInfo.setCity(fakerWithCN.address().city());
        userInfo.setStreet(fakerWithCN.address().fullAddress());
        userInfo.setUniversityName(fakerWithCN.university().name());
        copyOnWriteList.add(userInfo);
    }
    System.out.println(JSON.toJSONString(copyOnWriteList));
}
我们来看下结果:
[{"city":"石家庄","phone":"14586522683","realName":"孟泽洋","street":"马栋888号, 吉林, 甘 874323","universityName":"西北大学","userId":4923657},
{"city":"大连","phone":"15127458591","realName":"龙明杰","street":"戴桥44188号, 常德, 蒙 579884","universityName":"西南理工大学","userId":280654770},
{"city":"荣成","phone":"13308585499","realName":"胡鑫磊","street":"胡街83712号, 揭阳, 青 033123","universityName":"西艺术大学","userId":632},
{"city":"张家口","phone":"17726124577","realName":"钱弘文","street":"Suite 194 韦侬8853号, 潮州, 粤 708864","universityName":"南理工大学","userId":253101517},
{"city":"嘉峪关","phone":"17574031996","realName":"姚荣轩","street":"Apt. 681 崔桥0号, 文登, 黑 662009","universityName":"北农业大学","userId":12},
{"city":"宜兴","phone":"15804635509","realName":"高荣轩","street":"Apt. 571 方栋11号, 台州, 晋 398879","universityName":"西北大学","userId":61887},
{"city":"宜兴","phone":"17671687503","realName":"龙炫明","street":"余旁68号, 丽水, 港 289411","universityName":"西南理工大学","userId":72},
{"city":"南宁","phone":"17292050337","realName":"郝立轩","street":"冯旁1号, 抚顺, 渝 817418","universityName":"东北科技大学","userId":734142711},
{"city":"芜湖","phone":"15807366900","realName":"高风华","street":"罗街84735号, 宝鸡, 鲁 507848","universityName":"西南大学","userId":742},
{"city":"宜兴","phone":"17241054433","realName":"邵金鑫","street":"宋侬477号, 潍坊, 黑 307888","universityName":"东南理工大学","userId":11},
{"city":"株洲","phone":"15047199665","realName":"顾博文","street":"郝巷5号, 渭南, 皖 745439","universityName":"南理工大学","userId":18019627},
{"city":"合肥","phone":"13144098426","realName":"孔君浩","street":"Suite 389 贾侬56308号, 盐城, 宁 041066","universityName":"西理工大学","userId":99},
{"city":"临沂","phone":"14714102564","realName":"李致远","street":"Suite 005 林桥62713号, 文登, 苏 722324","universityName":"东北体育大学","userId":51989707},
{"city":"舟山","phone":"14577628241","realName":"秦伟诚","street":"赖街1号, 镇江, 蒙 115625","universityName":"东南技术大学","userId":43156895},
{"city":"宜宾","phone":"18024394411","realName":"方立诚","street":"Suite 799 郭桥15021号, 洛阳, 豫 584630","universityName":"西北体育大学","userId":78836},
{"city":"绵阳","phone":"17064325265","realName":"龙天磊","street":"Apt. 530 韦栋05号, 泸州, 鲁 251822","universityName":"北大学","userId":490253},
{"city":"烟台","phone":"15857473652","realName":"白钰轩","street":"Suite 948 彭路70号, 汕头, 港 137866","universityName":"南技术大学","userId":2409758},
{"city":"哈尔滨","phone":"15156367728","realName":"邵俊驰","street":"Apt. 333 马街328号, 威海, 青 995114","universityName":"中国农业大学","userId":4907},
{"city":"秦皇岛","phone":"14724465738","realName":"雷智辉","street":"Apt. 098 王桥0号, 宜兴, 川 677962","universityName":"西南技术大学","userId":768915},
{"city":"宜兴","phone":"17275815419","realName":"苏鹤轩","street":"张巷651号, 清远, 粤 027956","universityName":"西北经贸大学","userId":208008}]
以上json是javafaker生成的仿真数据

是不是看上去跟真的一样,但是一定要用到正途,不要用到歪门邪道上。

3. 总结

用了javafaker之后我们不但可以完成老板提出的需求,而且可以减少我们平常使用测试的时候用各种test填充的局面。
好了今天的分享就到这里。我是 土豆丝不辣,多多关注,获取更多有用的编程干货。












有道词典
public static v ...
详细X
公共静态void main (String [] args) {

本文共计 4958 字,感谢您的耐心浏览与评论。

声明:土豆丝不辣|版权所有,违者必究|如未注明,均为原创|转载请注明原文链接说明出处

0条回应:“领导居然让我用Java造假数据”