Skip to content

Commit 2fbcb9b

Browse files
AssertJ Demo Tests
1 parent e7ade78 commit 2fbcb9b

File tree

6 files changed

+200
-8
lines changed

6 files changed

+200
-8
lines changed

Sample.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"countries":{
3+
"continent": [
4+
{
5+
"country": "India",
6+
"city": "Hyderabad"
7+
},
8+
{
9+
"country": "Germany",
10+
"city": "Berlin"
11+
}
12+
],
13+
"region": "EuroAsia"
14+
}
15+
}

Sample.txt

Whitespace-only changes.

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<appium.version>9.0.0</appium.version>
1818
<testng.version>7.8.0</testng.version>
1919
<assertJ.version>3.24.2</assertJ.version>
20+
<joda.time.version>2.12.5</joda.time.version>
21+
<assertJ.joda.verson>2.2.0</assertJ.joda.verson>
2022
</properties>
2123

2224
<dependencies>
@@ -53,6 +55,18 @@
5355
<version>${assertJ.version}</version>
5456
<scope>test</scope>
5557
</dependency>
58+
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
59+
<dependency>
60+
<groupId>joda-time</groupId>
61+
<artifactId>joda-time</artifactId>
62+
<version>${joda.time.version}</version>
63+
</dependency>
64+
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-joda-time -->
65+
<dependency>
66+
<groupId>org.assertj</groupId>
67+
<artifactId>assertj-joda-time</artifactId>
68+
<version>${assertJ.joda.verson}</version>
69+
<scope>test</scope>
70+
</dependency>
5671
</dependencies>
57-
5872
</project>

src/main/java/com/playground/demo/assertJ/AssertJ.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package demo.assertJ;
2+
3+
import org.assertj.core.data.Offset;
4+
import org.assertj.core.data.Percentage;
5+
import org.assertj.jodatime.api.Assertions;
6+
import org.joda.time.LocalDate;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.*;
11+
12+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
13+
import static org.assertj.core.api.BDDAssertions.assertThat;
14+
import static org.assertj.core.api.BDDAssertions.then;
15+
16+
17+
public class AssertJ {
18+
public static void main(String[] args) throws IOException {
19+
//Equal Assertions
20+
assertThat("John Doe").isEqualTo("John Doe");
21+
assertThat("John").isNotEqualTo("Doe");
22+
23+
//Null Assertions
24+
String value="John";
25+
String nullValue = null;
26+
assertThat(value).as("Non Null Check").isNotNull();
27+
assertThat(nullValue).as("Null Check").isNull();
28+
29+
//Boolean Assertions
30+
Boolean boolValue = true;
31+
assertThat(boolValue).isTrue();
32+
assertThat(boolValue).isNotEqualTo(false);
33+
34+
//String Assertions - Chaining the Assertions
35+
String strValue="Happy New Year";
36+
assertThat(strValue)
37+
.describedAs("String Assertions")
38+
.isNotNull()
39+
.startsWith("Happy")
40+
.contains("New")
41+
.endsWith("Year")
42+
.isNotBlank()
43+
.isNotEmpty()
44+
.overridingErrorMessage(()->"Custom Message..")
45+
.containsWhitespaces()
46+
.doesNotContainOnlyWhitespaces()
47+
.isMixedCase()
48+
.containsIgnoringWhitespaces("HappyNewYear")
49+
.hasSizeGreaterThan(5)
50+
.as("Input contains only the characters and space")
51+
.matches("^[a-zA-Z ]*")
52+
.describedAs("Input doesn't contain the numbers")
53+
.doesNotMatch("\\d.*")
54+
.isInstanceOf(String.class);
55+
assertThat("Game'of'Thrones").isEqualToNormalizingPunctuationAndWhitespace("GameofThrones");
56+
57+
//Numeric Assertions
58+
int number=58;
59+
assertThat(number)
60+
.describedAs("Numeric Assertions")
61+
.isPositive()
62+
.isNotNegative()
63+
.isNotZero()
64+
.isBetween(35,70)
65+
.isInstanceOf(Integer.class)
66+
.isEven()
67+
.isCloseTo(60, Offset.offset(2))
68+
.isCloseTo(52, Percentage.withPercentage(15))
69+
.isNotCloseTo(70,Percentage.withPercentage(5));
70+
71+
//List based Iterable Assertions
72+
List<String> languages= Arrays.asList("Java","Python","Ruby","Go");
73+
List<String> spokenLanguage=Arrays.asList("English","Deutsch");
74+
assertThat(languages)
75+
.hasSize(4)
76+
.hasSizeLessThan(6)
77+
.isNotEmpty()
78+
.isNotNull()
79+
.doesNotContainNull()
80+
.doesNotHaveDuplicates()
81+
.isInstanceOf(List.class)
82+
.doesNotContainAnyElementsOf(spokenLanguage)
83+
.withFailMessage(()->"Input doen't matches criteria")
84+
.allMatch(s->s.matches("^[a-zA-Z]*"))
85+
.containsExactly("Java","Python","Ruby","Go");
86+
87+
//Map based Assertions
88+
Map<String, String> sports=new HashMap<>();
89+
sports.put("Cricket","England");
90+
sports.put("Football","Germany");
91+
sports.put("Hockey","India");
92+
assertThat(sports)
93+
.hasSize(3)
94+
.containsKey("Cricket")
95+
.containsValue("India")
96+
.containsEntry("Football","Germany")
97+
.isInstanceOf(Map.class)
98+
.isNotNull()
99+
.satisfies(sport -> assertThat(sport.size()>2));
100+
101+
//Object based Assertions
102+
Student s1=new Student("James",24,"Hyd");
103+
Student s2=new Student("John",28,"BLR");
104+
Student s3=new Student("Rosy",26,"Mum");
105+
assertThat(s1)
106+
.isInstanceOf(Student.class)
107+
.as("%s age should be less than 35",s1.getName())
108+
.hasFieldOrProperty("age")
109+
.hasFieldOrPropertyWithValue("city","Hyd")
110+
.hasNoNullFieldsOrProperties()
111+
.extracting(s -> s.getAge())
112+
.isEqualTo(24);
113+
114+
//File based Assertions
115+
File fileObj= new File(System.getProperty("user.dir").concat(System.getProperty("file.separator")+"Sample.txt"));
116+
assertThat(fileObj)
117+
.describedAs("File Assertions")
118+
.canRead()
119+
.canWrite()
120+
.isFile()
121+
.hasExtension("txt");
122+
123+
//Exception Assertions
124+
assertThatThrownBy(()->getException()).as("Check Exception").isInstanceOf(RuntimeException.class);
125+
assertThatThrownBy(()->{throw new Exception("foo");}).hasMessage("foo");
126+
127+
//Date and Time Based Assertions
128+
LocalDate date=new LocalDate(2023,1,1);
129+
Assertions.assertThat(date)
130+
.hasYear(2023)
131+
.hasMonthOfYear(1)
132+
.hasDayOfMonth(1)
133+
.isBefore(new LocalDate(2024,1,1));
134+
135+
//BDD Assertions
136+
then("AssertJ")
137+
.satisfies(val->{
138+
assertThat(val).isNotNull();
139+
assertThat(val.length()).isGreaterThan(4);
140+
});
141+
142+
}
143+
public static void getException(){
144+
throw new RuntimeException();
145+
}
146+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package demo.assertJ;
2+
3+
public class Student {
4+
5+
public Student(String name, int age, String city){
6+
this.name=name;
7+
this.age=age;
8+
this.city=city;
9+
}
10+
private String name;
11+
private int age;
12+
private String city;
13+
public String getName(){
14+
return this.name;
15+
}
16+
public int getAge(){
17+
return this.age;
18+
}
19+
public String getCity(){
20+
return this.city;
21+
}
22+
23+
24+
}

0 commit comments

Comments
 (0)