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