vastik/spring-java-faker


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

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.vastik:spring-data-faker:1.0.18'
	}
	dependencies {
		implementation("com.github.vastik:spring-data-faker:1.0.18")
	}
	<dependency>
	    <groupId>com.github.vastik</groupId>
	    <artifactId>spring-data-faker</artifactId>
	    <version>1.0.18</version>
	</dependency>

                            
    libraryDependencies += "com.github.vastik" % "spring-data-faker" % "1.0.18"
        
        

                            
    :dependencies [[com.github.vastik/spring-data-faker "1.0.18"]]
        
        

Readme


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.

<a name="usage"><h3>Basic usage</h3></a> 1. Add jitpack.io maven repository to your project: groovy repositories { maven { url "https://jitpack.io" } }

  1. Add library to your dependencies list (make sure you using last version):

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

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

<a name="annotations"><h3>Annotations</h3></a>

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

<a name="custom-faker"><h3>Customization</h3></a> If you want to fake custom data you should: 1. Create your annotation java @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FakeLeetNumber { } 2. Create class that will handle your annotation by implementing AnnotationHandler interface java public class FakeLeetAnnotationHandler implements AnnotationHandler<FakeLeetNumber> { @Override public Object get(FakeLeetNumber annotation, DataFakeContext context) throws Exception { return 1337; } } 3. Register your handler in DataFakerRegistry ```java @Configuration public class CustomDataFakerConfiguration implements InitializingBean { @Autowired private DataFaker dataFaker;

    public void afterPropertiesSet() {
        dataFaker.getRegistry().registerHandler(FakeLeetNumber.class, new FakeLeetAnnotationHandler());
    }   
}
```
  1. Use

    @FakeLeetNumber
    private Integer leet;