A Guide to JavaFaker – JavaFaker指南

最后修改: 2018年 8月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

JavaFaker is a library that can be used to generate a wide array of real-looking data from addresses to popular culture references.

JavaFaker是一个库,可用于生成从地址到流行文化参考资料等各种看起来很真实的数据。

In this tutorial, we’ll be looking at how to use JavaFaker’s classes to generate fake data. We’ll start by introducing the Faker class and the FakeValueService, before moving on to introducing locales to make the data more specific to a single place.

在本教程中,我们将研究如何使用JavaFaker的类来生成假数据。我们将首先介绍Faker类和FakeValueService,然后再介绍locales,以使数据更具体到一个地方。

Finally, we’ll discuss how unique the data is. To test JavaFaker’s classes, we’ll make use of regular expressions, you can read more about them here.

最后,我们将讨论数据的唯一性。为了测试JavaFaker的类,我们将利用正则表达式,你可以在这里阅读更多关于它们的信息

2. Dependencies

2.依赖性

Below is the single dependency we’ll need to get started with JavaFaker.

下面是我们开始使用JavaFaker所需要的单一依赖

First, the dependency we’ll need for Maven-based projects:

首先,我们需要基于Maven的项目的依赖性。

<dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.15</version>
</dependency>

For Gradle users, you can add the following to your build.gradle file:

对于Gradle用户,你可以在你的build.gradle文件中加入以下内容。

compile group: 'com.github.javafaker', name: 'javafaker', version: '0.15'

3. FakeValueService

3.FakeValueService

The FakeValueService class provides methods for generating random sequences as well as resolving .yml files associated with the locale.

FakeValueService类提供生成随机序列的方法以及解决与locale相关的.yml文件。

In this section, we’ll cover some of the useful methods that the FakerValueService has to offer.

在本节中,我们将介绍FakerValueService所提供的一些有用方法。

3.1. Letterify, Numerify, and Bothify

3.1.Letterify, Numerify, 和Bothify

Three useful methods are Letterify, Numberify, and Bothify. Letterify helps to generate random sequences of alphabetic characters.

三个有用的方法是Letterify, Numberify, 和BothifyLetterify有助于生成随机的字母字符序列

Next, Numerify simply generates numeric sequences.

接下来,Numerify简单地生成了数字序列。

Finally, Bothify is a combination of the two and can create random alphanumeric sequences – useful for mocking things like ID strings.

最后,Bothify是两者的结合,可以创建随机的字母数字序列–对于模拟ID字符串等东西很有用。

FakeValueService requires a valid Locale, as well as a RandomService:

FakeValueService需要一个有效的Locale,以及一个RandomService:

@Test
public void whenBothifyCalled_checkPatternMatches() throws Exception {

    FakeValuesService fakeValuesService = new FakeValuesService(
      new Locale("en-GB"), new RandomService());

    String email = fakeValuesService.bothify("????##@gmail.com");
    Matcher emailMatcher = Pattern.compile("\\w{4}\\d{2}@gmail.com").matcher(email);
 
    assertTrue(emailMatcher.find());
}

In this unit test, we create a new FakeValueService with a locale of en-GB and use the bothify method to generate a unique fake Gmail address.

在这个单元测试中,我们创建一个新的FakeValueService,其定位为en-GB,并且使用bothify方法来生成一个独特的假Gmail地址

It works by replacing ‘?’ with random letters and ‘#’ with random numbers. We can then check the output is correct with a simple Matcher check.

它的工作原理是:用随机字母替换’?’‘#’用随机数字替换。然后我们可以用一个简单的Matcher检查来检查输出是否正确。

3.2. Regexify

3.2 Regexify

Similarly, regexify generates a random sequence based on a chosen regex pattern.

同样,regexify根据选定的regex模式生成一个随机序列

In this snippet, we’ll use the FakeValueService to create a random sequence following a specified regex:

在这个片段中,我们将使用FakeValueService来创建一个遵循指定搜索结果的随机序列。

@Test
public void givenValidService_whenRegexifyCalled_checkPattern() throws Exception {

    FakeValuesService fakeValuesService = new FakeValuesService(
      new Locale("en-GB"), new RandomService());

    String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
    Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}").matcher(alphaNumericString);
 
    assertTrue(alphaNumericMatcher.find());
}

Our code creates a lower-case alphanumeric string of length 10. Our pattern checks the generated string against the regex.

我们的代码创建一个长度为10的小写字母数字字符串。我们的模式会根据重码检查生成的字符串。

4. JavaFaker’s Faker Class

4.JavaFaker的Faker

The Faker class allows us to use JavaFaker’s fake data classes.

Faker允许我们使用JavaFaker的假数据类

In this section, we’ll see how to instantiate a Faker object and use it to call some fake data:

在本节中,我们将看到如何实例化一个Faker对象并使用它来调用一些假数据。

Faker faker = new Faker();

String streetName = faker.address().streetName();
String number = faker.address().buildingNumber();
String city = faker.address().city();
String country = faker.address().country();

System.out.println(String.format("%s\n%s\n%s\n%s",
  number,
  streetName,
  city,
  country));

Above, we use the Faker Address object to generate a random address.

上面,我们使用Faker Address对象来生成一个随机地址

When we run this code, we’ll get an example of the output:

当我们运行这段代码时,我们会得到一个输出的例子。

3188
Dayna Mountains
New Granvilleborough
Tonga

We can see that the data has no single geographical location since we didn’t specify a locale. To change this, we’ll learn to make the data more relevant to our location in the next section.

我们可以看到,数据没有单一的地理位置,因为我们没有指定一个地区。为了改变这种情况,我们将在下一节学习如何使数据与我们的位置更相关。

We could also use this faker object in a similar way to create data relating to many more objects such as:

我们也可以用类似的方式使用这个faker对象来创建与许多其他对象有关的数据,例如。

  • Business
  • Beer
  • Food
  • PhoneNumber

You can find the full list here.

你可以找到完整的名单这里

5. Introducing Locales

5.介绍地方政府

Here, we’ll introduce how to use locales to make the generated data more specific to a single location. We’ll introduce a Faker with a US locale, and a UK locale:

在这里,我们将介绍如何使用locales来使生成的数据更具体地适用于一个地方。我们将介绍一个Faker的美国地区和英国地区。

@Test
public void givenJavaFakersWithDifferentLocals_thenHeckZipCodesMatchRegex() {

    Faker ukFaker = new Faker(new Locale("en-GB"));
    Faker usFaker = new Faker(new Locale("en-US"));

    System.out.println(String.format("American zipcode: %s", usFaker.address().zipCode()));
    System.out.println(String.format("British postcode: %s", ukFaker.address().zipCode()));

    Pattern ukPattern = Pattern.compile(
      "([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|"
      + "(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y]" 
      + "[0-9]?[A-Za-z]))))\\s?[0-9][A-Za-z]{2})");
    Matcher ukMatcher = ukPattern.matcher(ukFaker.address().zipCode());

    assertTrue(ukMatcher.find());

    Matcher usMatcher = Pattern.compile("^\\d{5}(?:[-\\s]\\d{4})?$")
      .matcher(usFaker.address().zipCode());

    assertTrue(usMatcher.find());
}

Above, we see that the two Fakers with the locale match their regexes for the countries zip codes.

在上面,我们看到两个带有locale的Fakers与他们对国家邮政编码的重码相匹配。

If the locale passed to the Faker does not exist, the Faker throws a LocaleDoesNotExistException.

如果传递给Faker的locale不存在,Faker会抛出一个LocaleDoesNotExistException

We’ll test this with the following unit test:

我们将用下面的单元测试来测试这一点。

@Test(expected = LocaleDoesNotExistException.class)
public void givenWrongLocale_whenFakerInitialised_testExceptionThrown() {
    Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
}

6. Uniqueness

6.独特之处

While JavaFaker seemingly generates data at Random, the uniqueness cannot be guaranteed.

虽然JavaFaker 似乎是随机生成的数据,但不能保证其唯一性

JavaFaker supports seeding of its pseudo-random number generator (PRNG) in the form of a RandomService to provide the deterministic output of repeated method calls.

JavaFaker支持以RandomService的形式对其伪随机数发生器(PRNG)进行播种,以提供重复方法调用的确定性输出。

Simply put, pseudorandomness is a process that appears random but is not.

简单地说,伪随机性是一个看起来是随机的,但不是的过程。

We can see how this works by creating two Fakers with the same seed:

我们可以通过用相同的种子创建两个Fakers来了解这个工作。

@Test
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() {

    Faker faker1 = new Faker(new Random(24));
    Faker faker2 = new Faker(new Random(24));

    assertEquals(faker1.name().firstName(), faker2.name().firstName());
}

The above code returns the same name from two different fakers.

上面的代码从两个不同的伪造者那里返回相同的名字

7. Conclusion

7.结语

In this tutorial, we have explored the JavaFaker library to generate real-looking fake data. We’ve also covered two useful classes the Faker class and the FakeValueService class.

在本教程中,我们探讨了JavaFaker库,以生成看起来真实的假数据。我们还介绍了两个有用的类:Faker类和FakeValueService类。

We explored how we can use locales to generate location specific data.

我们探讨了如何使用locales来生成特定地点的数据。

Finally, we discussed how the data generated only seems random and the uniqueness of the data is not guaranteed.

最后,我们讨论了生成的数据似乎只是随机的,而且数据的唯一性也得不到保证。

As usual, code snippets can be found over on GitHub.

像往常一样,可以在GitHub上找到代码片断。