java测试造数据神器JavaFaker
背景构造测试数据时,需要绞尽脑汁浪费时间,JavaFaker可以释放你的生产力githup地址安装<dependency><groupId>com.github.javafaker</groupId><artifactId>javafaker</artifactId><version>0.17.2</version>
·
背景
构造测试数据时,需要绞尽脑汁浪费时间,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)
英文就使用
Faker fakerWithCN = new Faker(Locale.US);
之后:
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}
转自:https://www.cnblogs.com/endv/p/12846917.html#top
欢迎关注我的公众号:(づ ̄3 ̄)づ╭❤~
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)