Java Faker Tests (#4922)

* Java Faker Tests

* Rename tests

* Rename Test Class

* Remove files from sub module
This commit is contained in:
daoire 2018-08-14 07:25:11 +01:00 committed by GitHub
parent 74e3e7ff95
commit 924a3dc9cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 126 additions and 144 deletions

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>java-faker</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,115 +0,0 @@
package com.baeldung;
import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.FakerIDN;
import com.github.javafaker.service.LocaleDoesNotExistException;
import com.github.javafaker.service.RandomService;
import javafx.scene.Parent;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class JavaFakerTest {
private Faker faker;
@Before
public void setUp() throws Exception {
faker = new Faker();
}
@Test
public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception {
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));
}
@Test
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception {
Faker faker1 = new Faker(new Random(24));
Faker faker2 = new Faker(new Random(24));
assertEquals(faker1.name().firstName(), faker2.name().firstName());
}
@Test
public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception {
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());
}
@Test
public void givenJavaFakerService_testFakersCreated() throws Exception {
RandomService randomService = new RandomService();
System.out.println(randomService.nextBoolean());
System.out.println(randomService.nextDouble());
Faker faker = new Faker(new Random(randomService.nextLong()));
System.out.println(faker.address().city());
}
@Test
public void testFakeValuesService() 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());
String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}").matcher(alphaNumericString);
assertTrue(alphaNumericMatcher.find());
}
@Test(expected = LocaleDoesNotExistException.class)
public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception {
Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
}
}

View File

@ -88,6 +88,11 @@
<artifactId>javalite-common</artifactId> <artifactId>javalite-common</artifactId>
<version>${javalite.version}</version> <version>${javalite.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.15</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,121 @@
package com.baeldung.javafaker;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.Test;
import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.LocaleDoesNotExistException;
import com.github.javafaker.service.RandomService;
public class JavaFakerUnitTest {
private Faker faker;
@Before
public void setUp() throws Exception {
faker = new Faker();
}
@Test
public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception {
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));
}
@Test
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception {
Faker faker1 = new Faker(new Random(24));
Faker faker2 = new Faker(new Random(24));
assertEquals(faker1.name()
.firstName(),
faker2.name()
.firstName());
}
@Test
public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception {
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());
}
@Test
public void givenJavaFakerService_testFakersCreated() throws Exception {
RandomService randomService = new RandomService();
System.out.println(randomService.nextBoolean());
System.out.println(randomService.nextDouble());
Faker faker = new Faker(new Random(randomService.nextLong()));
System.out.println(faker.address()
.city());
}
@Test
public void testFakeValuesService() 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());
String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}")
.matcher(alphaNumericString);
assertTrue(alphaNumericMatcher.find());
}
@Test(expected = LocaleDoesNotExistException.class)
public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception {
Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
}
}