Can acquire Faker instance via static utility methods (#237)

This commit is contained in:
Mantas Domaševičius
2017-06-09 09:45:43 +03:00
committed by Ricky Yim
parent 37e702c97d
commit fa5b4da233
2 changed files with 52 additions and 3 deletions
+43 -3
View File
@@ -67,7 +67,7 @@ public class Faker {
private final RickAndMorty rickAndMorty;
private final Yoda yoda;
private final Matz matz;
public Faker() {
this(Locale.ENGLISH);
}
@@ -137,6 +137,46 @@ public class Faker {
this.matz = new Matz(this);
}
/**
* Constructs Faker instance with default argument.
*
* @return {@link Faker#Faker()}
*/
public static Faker instance() {
return new Faker();
}
/**
* Constructs Faker instance with provided {@link Locale}.
*
* @param locale - {@link Locale}
* @return {@link Faker#Faker(Locale)}
*/
public static Faker instance(Locale locale) {
return new Faker(locale);
}
/**
* Constructs Faker instance with provided {@link Random}.
*
* @param random - {@link Random}
* @return {@link Faker#Faker(Random)}
*/
public static Faker instance(Random random) {
return new Faker(random);
}
/**
* Constructs Faker instance with provided {@link Locale} and {@link Random}.
*
* @param locale - {@link Locale}
* @param random - {@link Random}
* @return {@link Faker#Faker(Locale, Random)}
*/
public static Faker instance(Locale locale, Random random) {
return new Faker(locale, random);
}
/**
* Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive.
* <p>
@@ -397,7 +437,7 @@ public class Faker {
public Hipster hipster() {
return hipster;
}
public Job job() {
return job;
}
@@ -409,7 +449,7 @@ public class Faker {
public RickAndMorty rickAndMorty() {
return rickAndMorty;
}
public Yoda yoda() {
return yoda;
}
@@ -2,6 +2,7 @@ package com.github.javafaker;
import org.junit.Test;
import java.util.Locale;
import java.util.Random;
import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
@@ -153,4 +154,12 @@ public class FakerTest extends AbstractFakerTest {
final String resolve = faker.resolve("address.nothing");
assertThat(resolve, is(nullValue()));
}
@Test
public void fakerInstanceCanBeAcquiredViaUtilityMethods() {
assertThat(Faker.instance(), is(instanceOf(Faker.class)));
assertThat(Faker.instance(Locale.CANADA), is(instanceOf(Faker.class)));
assertThat(Faker.instance(new Random(1)), is(instanceOf(Faker.class)));
assertThat(Faker.instance(Locale.CHINA, new Random(2)), is(instanceOf(Faker.class)));
}
}