- Notifications
You must be signed in to change notification settings - Fork18
🏭Auto generate mock data for java test.(便于 Java 测试自动生成对象信息)
License
houbb/data-factory
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
我们平时在写测试用例的时候,免不了要写一大堆 set 方法为对象设置属性。
有时候为了补全测试用例,这件事就会变得非常枯燥。
于是就在想,能不能写一个可以自动生成测试对象的工具呢?
于是就有了这一个没啥用的测试框架:
data-factory 项目用于根据对象,随机自动生成初始化信息。便于测试。
8 大基本类型的支持
数组、对象、枚举、Map、链表、Set 等支持
String、BigDecimal、BigInteger、Currency 等常见类型的支持
Date、LocalDate、LocalDateTime、LocalTime、Year 等常见日期类型支持
支持 Regex 正则表达式
@DataFactory注解支持灵活配置
JDK 1.7+
Maven 3.0+
<dependency> <groupId>com.github.houbb</groupId> <artifactId>data-factory-core</artifactId> <version>1.2.0</version></dependency>
对应的 gradle 引入
implementation 'com.github.houbb:data-factory-core:1.2.0'我们通过DataUtil.build(class) 就可以生成对应类的随机值。
比如DataUtil.build(String.class);,就可以生成随机的字符串:
0s5Z8foS1当然,最常用的还是初始化一个 java 对象。
publicclassUser {privateStringname;privateintage;privateDatebirthday;privateList<String>stringList;//S/F 的枚举privateStatusEnumstatusEnum;privateMap<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 注解去进行限制。
publicclassUserAnnotationNumber {@DataFactory(min =10,max =20)privateByteaByte;@DataFactory(min =10,max =20)privateShortaShort;@DataFactory(min =10,max =20)privateIntegerinteger;@DataFactory(min =10,max =20)privateLongaLong;@DataFactory(min =10,max =20,precision =3)privateDoubleaDouble;@DataFactory(min =10,max =20,precision =3)privateFloataFloat;@DataFactory(min =10,max =20,precision =3)privateBigDecimalbigDecimal;@DataFactory(min =10,max =20)privateBigIntegerbigInteger;//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 支持用户自定义注解。
比如指定一个返回固定值的注解。
packagecom.github.houbb.data.factory.core.annotation;importcom.github.houbb.data.factory.api.annotation.meta.DataMeta;importjava.lang.annotation.*;/** * @author binbin.hou * @since 1.0.0 */@Inherited@Documented@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@DataMeta(value =AtMyStringAnnotationData.class)public @interfaceConstStringData {Stringvalue()default"";}
最重要的一点就是@DataMeta(value = AtMyStringAnnotationData.class);
@DataMeta 是一个最核心的元注解,value 对应的是具体实现。
AtMyStringAnnotationData 就是具体的注解实现,如下:
importcom.github.houbb.data.factory.api.core.IContext;importcom.github.houbb.data.factory.api.core.meta.IAnnotationData;publicclassAtMyStringAnnotationDataimplementsIAnnotationData<ConstStringData> {privateConstStringDataconstStringData;@Overridepublicvoidinitialize(ConstStringDataannotation) {constStringData =annotation; }@OverridepublicObjectbuild(IContextcontext,ClassaClass) {returnconstStringData.value(); }}
实现对应的 IAnnotationData 接口,initialize 初始化对应的注解信息。
build 构建对应的值。
定义好了注解,@ConstStringData 就可以如下使用了:
publicclassUserDefineAnnotationData {@ConstStringData("echo")privateStringname;@ConstStringData("game")privateStringhobby;// getter & setter}
UserDefineAnnotationDatadata =DataUtil.build(UserDefineAnnotationData.class);assertdata.getName().equals("echo");assertdata.getHobby().equals("game");
可以验证数据被初始化为对应的注解指定值。
支持多维数组
支持全局配置
About
🏭Auto generate mock data for java test.(便于 Java 测试自动生成对象信息)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.