This library allows you to generate fake data and customize it by using annotations. It uses java-faker as main fake data source and supports enumerations, collections and user-defined types.
-
Add jitpack.io maven repository to your project:
repositories { maven { url "https://jitpack.io" } }
-
Add library to your dependencies list (make sure you using last version):
compile 'com.github.vastik:spring-boot-starter-data-faker:1.0.+'
-
Annotate your class fields with @Fake-annotations, for example:
public class SimpleClass { @FakeRandom(15) private Integer count; @FakeFaker("gameOfThrones.dragon") private String name; @FakeValue({"RED", "BLACK"}) private Colors colors; @FakeRandom(25) @FakeCollection(min = 5, max = 15) private Set<Integer> integers; }
-
Use autowired DataFaker instance to fake data
@Autowired private DataFaker dataFaker; public void createSimpleClass() throws Exception { SimpleClass simpleClass = dataFaker.fake(SimpleClass.class); }
-
Faker method: date.future
Supported class: java.lang.Date, java.lang.LocalDateTime
@FakeDateFuture(value = 10, unit = TimeUnit.DAYS) private LocalDateTime dateOpen;
-
Faker method: date.past
Supported class: java.lang.Date, java.lang.LocalDateTime
@FakeDatePast(value = 5, unit = TimeUnit.DAYS) private LocalDateTime dateOpen;
-
Faker method: date.between
Supported class: java.lang.Date, java.lang.LocalDateTime
Notes: Use @FakePast and @FakeFuture annotations to define time interval.
@FakeDateBetween( past = @FakeDatePast(value = 5, unit = TimeUnit.DAYS), future = @FakeDateFuture(value = 10, unit = TimeUnit.DAYS) ) private LocalDateTime dateOpen;
-
Will call LocalDateTime.now() on this field.
Supported class: java.lang.Date, java.lang.LocalDateTime
@FakeNow private LocalDateTime dateOpen;
-
Faker method: letterfiy
Supported class: java.lang.String
@FakeLetterify("12??34") private String callerId;
-
Faker method: bothify
Supported class: java.lang.String
@FakeBothify("###???") private String callerId;
-
Faker method: numberify
Supported class: java.lang.String
@FakeBothify("ABC##EFG") private String callerId;
-
Faker method: Number::randomNumber
Supported class: Interger, Long, Short, Float, Double, Byte
@FakeRandom(15) private Integer updateInterval;
-
Faker method: Number::randomNumber(digits)
Supported class: Interger, Long, Short, Float, Double, Byte
@FakeRandomNumber(digits = 7) private Integer phone;
-
Faker method: Number::numberBetween
Supported class: Interger, Long, Short, Float, Double, Byte
@FakeNumberBetween(min = 15, max = 20) private Integer phone;
-
You can call custom faker method by using @FakeFaker annotation that takes faker method chain. For example:
@FakeFaker("gameOfThrones.dragon") private String name;
The only limitation here is that the calling method should not take any arguments.
-
Will apply specific values if provided or will apply random value from target enum class.
public enum Colors { RED, WHITE, BLACK } @FakeValue({"RED", "BLACK"}) private Colors colors;
-
If you want to fake custom classes in your class you can use @FakeInclude annotation. Applying this annotation on self-referenced or bi-directional classes will result in endless recursive process!
@FakeInclude private Contact contact;
-
Allows you to fake java.util.List or java.util.Set by placing @Fake-annotation that match generic type of your collection.
@FakeFaker("gameOfThrones.dragon") @FakeCollection(min = 5, max = 20) private List<String> dragonNames;
- Create your annotation
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FakeLeetNumber { }
- Create class that will handle your annotation by implementing AnnotationHandler interface
public class FakeLeetAnnotationHandler implements AnnotationHandler<FakeLeetNumber> { @Override public Object get(FakeLeetNumber annotation, DataFakeContext context) throws Exception { return 1337; } }
- Register your handler in DataFakerRegistry
@Configuration public class CustomDataFakerConfiguration implements InitializingBean { @Autowired private DataFaker dataFaker; public void afterPropertiesSet() { dataFaker.getRegistry().registerHandler(FakeLeetNumber.class, new FakeLeetAnnotationHandler()); } }
- Use
@FakeLeetNumber private Integer leet;