Faker Configuration
ToC
Default Configuration
Faker
can be configured through the FakerConfig
class, either by passing an instance of FakerConfig
directly, or
If no FakerConfig
instance is passed to Faker
constructor and configuration is not set through the faker
builder DSL, then default configuration will be used:
locale == "en"
random
is seeded with a pseudo-randomly generated number.uniqueGeneratorRetryLimit
is set to100
back-to-toc
Deterministic Random
Faker supports seeding of it's PRNG (pseudo-random number generator) through FakerConfig
to provide deterministic output of repeated function invocations.
val config = fakerConfig {
random = Random(42)
}
val faker = Faker(config)
val city1 = faker.address.city()
val name1 = faker.name.name()
val otherConfig = fakerConfig {
random = Random(42)
}
val otherFaker = Faker(otherConfig)
val city2 = otherFaker.address.city()
val name2 = otherFaker.name.name()
assertEquals(city1, city2)
assertEquals(name1, name2)
FakerConfig.Builder configBuilder = FakerConfig.builder();
Faker faker = new Faker(configBuilder.withRandom(new Random(42)).build());
String city1 = faker.getAddress().city();
String name1 = faker.getName().name();
Faker otherFaker = new Faker(configBuilder.withRandom(new Random(42)).build());
String city2 = otherFaker.getAddress().city();
String name2 = otherFaker.getName().name();
assertEquals(city1, city2);
assertEquals(name1, name2);
Alternatively a randomSeed
property can be used instead of passing an instance of java.util.Random
:
val config = fakerConfig {
randomSeed = 42
}
val faker = Faker(config)
FakerConfig config = FakerConfig.builder()
.withRandomSeed(42)
.build();
Faker faker = new Faker(config);
info
randomSeed
config property has precedence over random
property, and the latter will be ignored if randomSeed
is specified.
val config = fakerConfig {
random = Random(123)
randomSeed = 42
}
val faker = Faker(config)
val city1 = faker.address.city()
val otherConfig = fakerConfig {
random = Random(123)
randomSeed = 42
}
val otherFaker = Faker(otherConfig)
val city2 = otherFaker.address.city()
assertEquals(city1, city2)
FakerConfig config = FakerConfig.builder()
.withRandom(new Random(123))
.withRandomSeed(42)
.build();
Faker faker = new Faker(config);
String city1 = faker.getAddress().city();
FakerConfig otherConfig = FakerConfig.builder()
.withRandom(new Random(42))
.build();
Faker otherFaker = new Faker(otherConfig);
String city2 = otherFaker.getAddress().city();
assertEquals(city1, city2);
back-to-toc
Locale
By default Faker
uses en_US
-localized dict files to generate data:
val faker = Faker()
assertEquals(faker.address.defaultCountry(), "United States of America")
When needed, Faker
can be configured to use a custom localized dictionary file instead:
val config = fakerConfig {
locale = "nb-NO"
}
val faker = Faker(config)
assertEquals(faker.address.defaultCountry(), "Norge")
FakerConfig config = FakerConfig.builder()
.withLocale("nb-NO")
.build();
Faker faker = new Faker(config);
assertEquals(faker.getAddress().defaultCountry(), "Norge");
info
The locale
configuration property should be set following the .yml
dict file name, that is a -
should be used as a delimiter between "language" and "country" values instead of _
, i.e. nb-NO
instead of nb_NO
.
Using a non-default locale will replace the values in some of the providers with the values from localized dictionary.
val config = fakerConfig { locale = "es" }
val faker = Faker(config)
faker.address.city() // => Barcelona
info
Note that if the localized dictionary file does not contain a category (or a parameter in a category) that is present in the default locale, then non-localized (`en`) value will be generated instead.
val faker = Faker() // uses default 'en' locale
faker.gameOfThrones.cities() // => Braavos
val config = fakerConfig {
locale = "nb-NO"
}
val localizedFaker = Faker(config)
// 'game_of_thrones' category is not localized for `nb-NO` locale
// and non-localized value is returned instead
localizedFaker.gameOfThrones.cities() // => Braavos
A list of all locales, and their corresponding dictionary files, can be found on the Available Locales page.
back-to-toc
Random Class Instance
RandomClassInstance can also be configured both from the FakerConfig
level.
info
This configuration takes the least precedence and will be overridden by config set on the `faker.randomProvider` level or on the `randomClassInstance` function level. See [Random Class Instance Configuration](https://serpro69.github.io/kotlin-faker/wiki/extras#random-class-instance-configuration) for more details.
back-to-toc