XXXXX
C/C++
XXXXX
C#/.net
XXXXX
js
java
java
开发导航 开发导航 www.endv.cn
天云

java测试造数据神器JavaFaker

背景
构造测试数据时,需要绞尽脑汁浪费时间,JavaFaker可以释放你的生产力
 
githup地址

安装

<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.17.2</version>
</dependency>

 


 
使用
随机生成一百个学生及考生分数

学生模型

/**
* 测试model 学生
* @author szhu
*/
public class Student implements Comparable<Student>{

/**
* 姓名
*/
private String name;


/**
* 分数
*/
private double score;

public Student(String name, double score) {
this.name = name;
this.score = score;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getScore() {
return score;
}

public void setScore(double score) {
this.score = score;
}

@Override
public int compareTo(Student o) {
return Double.compare(this.score, o.score);
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
} 

 

2、指定汉语

Faker FAKER = new Faker(Locale.CHINA);
中文姓名
FAKER.name().fullName();
1~100之间两位小数数字
FAKER.number().randomDouble(2, 1, 100)

 


 
具体如下:

import com.github.javafaker.Faker;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* 模型生成工厂
*
* @author szhu
*/
public class ModelFactory {
/**
* faker 指定汉语,默认英语
*/
private static Faker FAKER = new Faker(Locale.CHINA);


/**
* 随机生成一定数量学生
*
* @param number 数量
* @return 学生
*/
public static List<Student> listStudentList(final int number) {
return Stream.generate(() -> new Student(FAKER.name().fullName(), FAKER.number().randomDouble(2, 1, number))).limit(number).collect(Collectors.toList());
}


/**
* main函数
*/
public static void main(String[] args) throws Exception {
listStudentList(100).forEach(System.out::println);
}

} 

 

运行效果如下
Student{name='余立轩', score=73.13}
Student{name='吴嘉熙', score=56.27}
Student{name='孙瑞霖', score=66.46}
Student{name='张志强', score=90.39}
Student{name='卢烨磊', score=29.98}
Student{name='于健雄', score=77.65}
Student{name='钱伟祺', score=72.74}
Student{name='郑思', score=26.2}
Student{name='张弘文', score=36.61}
Student{name='尹伟泽', score=55.85}
Student{name='高明辉', score=62.03}
Student{name='谢文', score=36.15}
Student{name='吴浩', score=65.94}
Student{name='赵文', score=1.88}
Student{name='苏鹏飞', score=17.84}
Student{name='钱峻熙', score=8.81}
Student{name='邹烨伟', score=30.07}
Student{name='吴明哲', score=46.42}
Student{name='邓伟宸', score=54.28}
Student{name='唐健雄', score=70.23}
Student{name='董思淼', score=43.5}
Student{name='贾昊强', score=20.77}
Student{name='郭鹏', score=26.25}
Student{name='金晋鹏', score=25.16}
Student{name='武绍辉', score=20.46}
Student{name='秦立轩', score=83.37}
Student{name='任文昊', score=55.13}
Student{name='徐哲瀚', score=20.26}
Student{name='郭昊然', score=58.53}
Student{name='叶峻熙', score=26.48}
Student{name='李天磊', score=30.1}
Student{name='苏荣轩', score=32.88}
Student{name='朱鑫鹏', score=56.78}
Student{name='莫越彬', score=18.77}
Student{name='毛烨华', score=11.39}
Student{name='龙驰', score=9.28}
Student{name='金昊天', score=7.99}
Student{name='叶博涛', score=28.7}
Student{name='孟文轩', score=81.7}
Student{name='高智渊', score=62.83}
Student{name='邵思远', score=51.35}
Student{name='黄鹏涛', score=32.37}
Student{name='何瑞霖', score=77.09}
Student{name='侯煜城', score=34.92}
Student{name='赖明杰', score=38.4}
Student{name='方明', score=79.8}
Student{name='毛黎昕', score=66.07}
Student{name='武志泽', score=50.99}
Student{name='赵修洁', score=13.2}
Student{name='苏擎苍', score=9.22}
Student{name='韩晓博', score=37.48}
Student{name='萧烨伟', score=15.73}
Student{name='江立果', score=23.78}
Student{name='严苑博', score=75.86}
Student{name='魏果', score=1.28}
Student{name='董昊然', score=72.64}
Student{name='唐越彬', score=14.42}
Student{name='陈修洁', score=68.48}
Student{name='吕君浩', score=4.67}
Student{name='姚锦程', score=13.18}
Student{name='钱思', score=88.58}
Student{name='萧鸿煊', score=2.99}
Student{name='方鹤轩', score=87.83}
Student{name='张峻熙', score=27.47}
Student{name='宋鹏煊', score=27.45}
Student{name='魏伟泽', score=73.5}
Student{name='徐熠彤', score=18.0}
Student{name='侯雪松', score=29.99}
Student{name='罗明辉', score=62.75}
Student{name='谭烨伟', score=65.24}
Student{name='汪君浩', score=83.27}
Student{name='段立辉', score=14.18}
Student{name='秦鸿煊', score=12.54}
Student{name='邵振家', score=45.83}
Student{name='孔立果', score=48.19}
Student{name='蒋哲瀚', score=23.36}
Student{name='龚熠彤', score=22.79}
Student{name='苏思源', score=29.13}
Student{name='秦浩轩', score=71.87}
Student{name='孔琪', score=65.91}
Student{name='顾彬', score=73.58}
Student{name='陈子涵', score=17.82}
Student{name='沈鹏飞', score=33.55}
Student{name='孟伟祺', score=13.03}
Student{name='钱立诚', score=38.05}
Student{name='黄博超', score=68.61}
Student{name='郝瑾瑜', score=18.91}
Student{name='林金鑫', score=64.76}
Student{name='夏笑愚', score=49.82}
Student{name='邱睿渊', score=2.62}
Student{name='孔伟诚', score=39.17}
Student{name='叶浩然', score=85.48}
Student{name='袁晟睿', score=89.67}
Student{name='龙瑞霖', score=10.89}
Student{name='贾弘文', score=22.21}
Student{name='方俊驰', score=63.3}
Student{name='刘鹏飞', score=30.46}
Student{name='苏烨华', score=40.82}
Student{name='杨浩宇', score=12.27}
Student{name='金立辉', score=80.11}
  

posted @ 2020-05-08 00:35  Endv  阅读(3608)  评论(0编辑  收藏  举报