BAEL-2042 JavaFaker unit tests
This commit is contained in:
parent
4ee81a2fd9
commit
a92283f0dd
|
@ -0,0 +1,29 @@
|
|||
<?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>
|
|
@ -0,0 +1,115 @@
|
|||
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"));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue