godwinnnnn / spring-java-faker

https://github.com/DiUS/java-faker integration for Spring Boot Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spring-data-faker

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.

Basic usage

  1. Add jitpack.io maven repository to your project:

    repositories {
        maven { url "https://jitpack.io" }
    }
  2. Add library to your dependencies list (make sure you using last version):

    compile 'com.github.vastik:spring-boot-starter-data-faker:1.0.+'
  3. 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;
    }
  4. Use autowired DataFaker instance to fake data

    @Autowired
    private DataFaker dataFaker;
    
    public void createSimpleClass() throws Exception {
        SimpleClass simpleClass = dataFaker.fake(SimpleClass.class);
    } 

Annotations

  • @FakeDateFuture

    Faker method: date.future

    Supported class: java.lang.Date, java.lang.LocalDateTime

    @FakeDateFuture(value = 10, unit = TimeUnit.DAYS)
    private LocalDateTime dateOpen;
  • @FakeDatePast

    Faker method: date.past

    Supported class: java.lang.Date, java.lang.LocalDateTime

    @FakeDatePast(value = 5, unit = TimeUnit.DAYS)
    private LocalDateTime dateOpen;
  • @FakeDateBetween

    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;
  • @FakeDateNow

    Will call LocalDateTime.now() on this field.

    Supported class: java.lang.Date, java.lang.LocalDateTime

    @FakeNow
    private LocalDateTime dateOpen;
  • @FakeLetterify

    Faker method: letterfiy

    Supported class: java.lang.String

    @FakeLetterify("12??34")
    private String callerId;
  • @FakeBothify

    Faker method: bothify

    Supported class: java.lang.String

    @FakeBothify("###???")
    private String callerId;
  • @FakeNumberify

    Faker method: numberify

    Supported class: java.lang.String

    @FakeBothify("ABC##EFG")
    private String callerId;
  • @FakeNumberRandom

    Faker method: Number::randomNumber

    Supported class: Interger, Long, Short, Float, Double, Byte

    @FakeRandom(15)
    private Integer updateInterval;
  • @FakeNumberDigits

    Faker method: Number::randomNumber(digits)

    Supported class: Interger, Long, Short, Float, Double, Byte

    @FakeRandomNumber(digits = 7)
    private Integer phone;
  • @FakeNumberBetween

    Faker method: Number::numberBetween

    Supported class: Interger, Long, Short, Float, Double, Byte

    @FakeNumberBetween(min = 15, max = 20)
    private Integer phone;
  • @FakeFaker

    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.

  • @FakeEnum

    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;
  • @FakeInclude

    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;
  • @FakeCollection

    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;

Customization

If you want to fake custom data you should:

  1. Create your annotation
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface FakeLeetNumber {
    }
  2. 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;
        }
    }
  3. 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());
        }   
    }
  4. Use
    @FakeLeetNumber
    private Integer leet;

About

https://github.com/DiUS/java-faker integration for Spring Boot Framework

License:MIT License


Languages

Language:Java 100.0%