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"]]
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" } }
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);
}
<a name="annotations"><h3>Annotations</h3></a>
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;
<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());
}
}
```
Use
@FakeLeetNumber
private Integer leet;