Custom providers¶
Since version 1.2.0 of Datafaker it's possible create your own provider of data.
A full example can be found in the source code.
Custom hardcoded provider¶
To create a custom provider of data, you'll need to do the following steps:
- Create custom provider of data
- Create your own custom faker which extends
Faker
and register custom provider
In code, this would look like the following:
Hardcoded provider¶
Create a custom provider of data:
public class Insect extends AbstractProvider<BaseProviders> {
private static final String[] INSECT_NAMES = new String[]{"Ant", "Beetle", "Butterfly", "Wasp"};
public Insect(BaseProviders faker) {
super(faker);
}
public String nextInsectName() {
return INSECT_NAMES[faker.random().nextInt(INSECT_NAMES.length)];
}
}
Register provider¶
Create your own custom faker, which extends Faker
, and register the custom provider:
Usage¶
To use the custom faker, you can do the following:
This will print something like the following:
Custom provider using Yaml file¶
In case you have a large set of data to load, it might be better to use a Yaml file.
To create a custom provider of data fom a file, you'll need to do the following steps:
- Create a custom provider of data
- Create your own custom faker which extends
Faker
and register custom provider
Yaml provider¶
First, create the custom provider which loads the data from a file:
public class InsectFromFile extends AbstractProvider<BaseProviders> {
private static final String KEY = "insectsfromfile";
public InsectFromFile(BaseProviders faker) {
super(faker);
faker.addPath(Locale.ENGLISH, Paths.get("src/test/ants.yml"));
faker.addPath(Locale.ENGLISH, Paths.get("src/test/bees.yml"));
}
public String ant() {
return resolve(KEY + ".ants");
}
public String bee() {
return resolve(KEY + ".bees");
}
}
The ants.yml
would look like the following:
en:
faker:
insectsfromfile:
ants: ['Driver ant', 'Fire ant', 'Harvester ant', 'Honey ant', 'Leafcutter ant', 'Sahara desert ant']
And if you want to use multiple YAML files, the bees.yml
would look like this:
en:
faker:
insectsfromfile:
bees: ['Bumblebee', 'Euglossine bee', 'Honeybee', 'Carpenter bee', 'Leaf-cutter bee', 'Mining bee']
Register provider¶
Registering the provider would happen like this:
Usage¶
To use the custom faker, you can do the following:
This will print something like the following: