创作目标
咱们平时在写测试用例的时候,免不了要写一大堆 set 办法为对象设置属性。
有时候为了补全测试用例,这件事就会变得十分干燥。
于是就在想,能不能写一个能够主动生成测试对象的工具呢?
于是就有了这一个没啥用的测试框架:
https://github.com/houbb/data-factory
我的项目简介
data-factory 我的项目用于依据对象,随机主动生成初始化信息。便于测试。
个性
- 8 大根本类型的反对
- 数组、对象、枚举、Map、链表、Set 等反对
- String、BigDecimal、BigInteger、Currency 等常见类型的反对
- Date、LocalDate、LocalDateTime、LocalTime、Year 等常见日期类型反对
- 反对 Regex 正则表达式
@DataFactory
注解反对灵便配置
变更日志
变更日志
疾速开始
筹备工作
JDK 1.8+
Maven 3.0+
maven 引入
<dependency> <groupId>com.github.houbb</groupId> <artifactId>data-factory-core</artifactId> <version>1.0.0</version></dependency>
根本类型
咱们通过 DataUtil.build(class)
就能够生成对应类的随机值。
比方 DataUtil.build(String.class);
,就能够生成随机的字符串:
0s5Z8foS1
对象
当然,最罕用的还是初始化一个 java 对象。
public class User { private String name; private int age; private Date birthday; private List<String> stringList; //S/F 的枚举 private StatusEnum statusEnum; private Map<String, String> map; //Getter & Setter}
构建办法 User user = DataUtil.build(User.class);
构建对象如下:
User{name='wZ8CJZtK', age=-564106861, birthday=Wed Feb 27 22:14:34 CST 2019, stringList=[Du4iJkQj], statusEnum=S, map={yA5yDqM=Kdzi}}
内容每次都随机,便于根本的测试数据填充。
@DataFactory
注解
当然,有时候咱们心愿生成的数据合乎肯定的规定,这个时候能够通过 @DataFactory
注解去进行限度。
详情见 DataFactory 注解反对
对象定义
public class UserAnnotationNumber { @DataFactory(min = 10, max = 20) private Byte aByte; @DataFactory(min = 10, max = 20) private Short aShort; @DataFactory(min = 10, max = 20) private Integer integer; @DataFactory(min = 10, max = 20) private Long aLong; @DataFactory(min = 10, max = 20, precision = 3) private Double aDouble; @DataFactory(min = 10, max = 20, precision = 3) private Float aFloat; @DataFactory(min = 10, max = 20, precision = 3) private BigDecimal bigDecimal; @DataFactory(min = 10, max = 20) private BigInteger bigInteger; //getter & setter}
成果
通过 DataUtil.build(UserAnnotationNumber.class)
生成的对象如下:
UserAnnotationNumber{aByte=10, aShort=17, integer=19, aLong=11, aDouble=19.888, aFloat=10.067, bigDecimal=18.035, bigInteger=13}
自定义注解反对
为了更加灵便的指定生成,最大水平的重用自定义策略。
v1.0.0 反对用户自定义注解。
自定义实现
注解定义
比方指定一个返回固定值的注解。
package com.github.houbb.data.factory.core.annotation;import com.github.houbb.data.factory.api.annotation.meta.DataMeta;import java.lang.annotation.*;/** * @author binbin.hou * @since 1.0.0 */@Inherited@Documented@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@DataMeta(value = AtMyStringAnnotationData.class)public @interface ConstStringData { String value() default "";}
最重要的一点就是 @DataMeta(value = AtMyStringAnnotationData.class)
;
@DataMeta
是一个最外围的元注解,value 对应的是具体实现。
具体实现
AtMyStringAnnotationData 就是具体的注解实现,如下:
import com.github.houbb.data.factory.api.core.IContext;import com.github.houbb.data.factory.api.core.meta.IAnnotationData;public class AtMyStringAnnotationData implements IAnnotationData<ConstStringData> { private ConstStringData constStringData; @Override public void initialize(ConstStringData annotation) { constStringData = annotation; } @Override public Object build(IContext context, Class aClass) { return constStringData.value(); }}
实现对应的 IAnnotationData 接口,initialize 初始化对应的注解信息。
build 构建对应的值。
注解应用
定义好了注解,@ConstStringData
就能够如下应用了:
public class UserDefineAnnotationData { @ConstStringData("echo") private String name; @ConstStringData("game") private String hobby; // getter & setter}
测试验证
UserDefineAnnotationData data = DataUtil.build(UserDefineAnnotationData.class);assert data.getName().equals("echo");assert data.getHobby().equals("game");
能够验证数据被初始化为对应的注解指定值。