Creation purpose
When we usually write test cases, we inevitably have to write a lot of set methods to set properties for objects.
Sometimes in order to complete the test case, this matter will become very boring.
So I wondered, can I write a tool that can automatically generate test objects?
So there is this useless test framework:
https://github.com/houbb/data-factory
Project Description
data-factory The project is used to randomly automatically generate initialization information according to the object. Easy to test.
characteristic
- 8 basic types of support
- Array, object, enumeration, Map, linked list, Set, etc. support
- String, BigDecimal, BigInteger, Currency and other common types support
- Date, LocalDate, LocalDateTime, LocalTime, Year and other common date types support
- Support Regex regular expression
@DataFactory
annotation supports flexible configuration
Change log
change log
Quick start
Ready to work
JDK 1.8+
Maven 3.0+
Introduced by maven
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>data-factory-core</artifactId>
<version>1.0.0</version>
</dependency>
basic type
We can generate the random value of the corresponding class DataUtil.build(class)
For example, DataUtil.build(String.class);
, you can generate a random string:
0s5Z8foS1
Object
Of course, the most commonly used is to initialize a java object.
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
}
Construction method User user = DataUtil.build(User.class);
The construction objects are as follows:
User{name='wZ8CJZtK', age=-564106861, birthday=Wed Feb 27 22:14:34 CST 2019, stringList=[Du4iJkQj], statusEnum=S, map={yA5yDqM=Kdzi}}
The content is random every time, which is convenient for basic test data filling.
@DataFactory
comment
Of course, sometimes we want the generated data to comply with certain rules. At this time, we can use the @DataFactory
annotation to restrict it.
For details, see DataFactory Annotation Support
Object definition
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
}
Effect
The objects generated by DataUtil.build(UserAnnotationNumber.class)
are as follows:
UserAnnotationNumber{aByte=10, aShort=17, integer=19, aLong=11, aDouble=19.888, aFloat=10.067, bigDecimal=18.035, bigInteger=13}
Custom annotation support
In order to specify the generation more flexibly, the custom strategy is reused to the greatest extent.
v1.0.0 supports user-defined annotations.
Custom implementation
Annotation definition
For example, specify an annotation that returns a fixed value.
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 "";
}
The most important point is @DataMeta(value = AtMyStringAnnotationData.class)
;
@DataMeta
is a core meta-annotation, and value corresponds to a specific implementation.
Implementation
AtMyStringAnnotationData is the specific annotation implementation, as follows:
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();
}
}
Implement the corresponding IAnnotationData interface and initialize the corresponding annotation information.
Build the value corresponding to the build.
Annotation use
After defining the annotation, @ConstStringData
can be used as follows:
public class UserDefineAnnotationData {
@ConstStringData("echo")
private String name;
@ConstStringData("game")
private String hobby;
// getter & setter
}
Test verification
UserDefineAnnotationData data = DataUtil.build(UserDefineAnnotationData.class);
assert data.getName().equals("echo");
assert data.getHobby().equals("game");
It can be verified that the data is initialized to the corresponding annotation specified value.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。