Skip to content

Commit 4fcef9f

Browse files
authored
Merge pull request eugenp#4820 from MrSamMillington/master
BAEL-eugenp#2042 Unit tests for java-faker article
2 parents 738e3af + 015240a commit 4fcef9f

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

‎testing-modules/java-faker/pom.xml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>java-faker</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.github.javafaker</groupId>
14+
<artifactId>javafaker</artifactId>
15+
<version>0.15</version>
16+
</dependency>
17+
18+
<!-- https://mvnrepository.com/artifact/junit/junit -->
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
<scope>test</scope>
24+
</dependency>
25+
26+
</dependencies>
27+
28+
29+
</project>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)