|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import com.github.javafaker.Faker; |
| 4 | +import com.github.javafaker.service.FakeValuesService; |
| 5 | +import com.github.javafaker.service.FakerIDN; |
| 6 | +import com.github.javafaker.service.LocaleDoesNotExistException; |
| 7 | +import com.github.javafaker.service.RandomService; |
| 8 | +import javafx.scene.Parent; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.rules.ExpectedException; |
| 13 | + |
| 14 | +import java.util.Locale; |
| 15 | +import java.util.Random; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertThat; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +public class JavaFakerTest { |
| 24 | + |
| 25 | + private Faker faker; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setUp() throws Exception { |
| 29 | + faker = new Faker(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception { |
| 34 | + |
| 35 | + Faker faker = new Faker(); |
| 36 | + |
| 37 | + String streetName = faker.address().streetName(); |
| 38 | + String number = faker.address().buildingNumber(); |
| 39 | + String city = faker.address().city(); |
| 40 | + String country = faker.address().country(); |
| 41 | + |
| 42 | + System.out.println(String.format("%s\n%s\n%s\n%s", |
| 43 | + number, |
| 44 | + streetName, |
| 45 | + city, |
| 46 | + country)); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception { |
| 52 | + |
| 53 | + Faker faker1 = new Faker(new Random(24)); |
| 54 | + Faker faker2 = new Faker(new Random(24)); |
| 55 | + |
| 56 | + assertEquals(faker1.name().firstName(), faker2.name().firstName()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception { |
| 61 | + |
| 62 | + Faker ukFaker = new Faker(new Locale("en-GB")); |
| 63 | + Faker usFaker = new Faker(new Locale("en-US")); |
| 64 | + |
| 65 | + System.out.println(String.format("American zipcode: %s", usFaker.address().zipCode())); |
| 66 | + System.out.println(String.format("British postcode: %s", ukFaker.address().zipCode())); |
| 67 | + |
| 68 | + 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})"); |
| 69 | + Matcher ukMatcher = ukPattern.matcher(ukFaker.address().zipCode()); |
| 70 | + |
| 71 | + assertTrue(ukMatcher.find()); |
| 72 | + |
| 73 | + Matcher usMatcher = Pattern.compile("^\\d{5}(?:[-\\s]\\d{4})?$").matcher(usFaker.address().zipCode()); |
| 74 | + |
| 75 | + assertTrue(usMatcher.find()); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenJavaFakerService_testFakersCreated() throws Exception { |
| 81 | + |
| 82 | + RandomService randomService = new RandomService(); |
| 83 | + |
| 84 | + System.out.println(randomService.nextBoolean()); |
| 85 | + System.out.println(randomService.nextDouble()); |
| 86 | + |
| 87 | + Faker faker = new Faker(new Random(randomService.nextLong())); |
| 88 | + |
| 89 | + System.out.println(faker.address().city()); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testFakeValuesService() throws Exception { |
| 95 | + |
| 96 | + FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new RandomService()); |
| 97 | + |
| 98 | + String email = fakeValuesService.bothify("????##@gmail.com"); |
| 99 | + Matcher emailMatcher = Pattern.compile("\\w{4}\\d{2}@gmail.com").matcher(email); |
| 100 | + assertTrue(emailMatcher.find()); |
| 101 | + |
| 102 | + String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}"); |
| 103 | + Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}").matcher(alphaNumericString); |
| 104 | + assertTrue(alphaNumericMatcher.find()); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + @Test(expected = LocaleDoesNotExistException.class) |
| 110 | + public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception { |
| 111 | + |
| 112 | + Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld")); |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments