Skip to content

Commit bea86d4

Browse files
authored
Merge pull request eugenp#455 from pivovarit/assertj-introduction
Assertj introduction
2 parents 2802971 + 4aa5f7f commit bea86d4

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎assertj-introduction/pom.xml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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>assertj-introduction</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.12</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.assertj</groupId>
19+
<artifactId>assertj-core</artifactId>
20+
<version>3.4.1</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.3</version>
31+
<configuration>
32+
<source>1.8</source>
33+
<target>1.8</target>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.assertj.domain;
2+
3+
public class Dog {
4+
private String name;
5+
private Float weight;
6+
7+
public Dog(String name, Float weight) {
8+
this.name = name;
9+
this.weight = weight;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public Float getWeight() {
17+
return weight;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.assertj.domain;
2+
3+
public class Person {
4+
private String name;
5+
private Integer age;
6+
7+
public Person(String name, Integer age) {
8+
this.name = name;
9+
this.age = age;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public Integer getAge() {
17+
return age;
18+
}
19+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.assertj;
2+
3+
import com.baeldung.assertj.domain.Dog;
4+
import com.baeldung.assertj.domain.Person;
5+
import org.junit.Ignore;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.NoSuchElementException;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.withPrecision;
14+
15+
public class AssertJDemoTest {
16+
17+
@Test
18+
public void whenComparingReferences_thenNotEqual() throws Exception {
19+
Dog fido = new Dog("Fido", 5.15f);
20+
Dog fidosClone = new Dog("Fido", 5.15f);
21+
22+
assertThat(fido).isNotEqualTo(fidosClone);
23+
}
24+
25+
@Test
26+
public void whenComparingFields_thenEqual() throws Exception {
27+
Dog fido = new Dog("Fido", 5.15f);
28+
Dog fidosClone = new Dog("Fido", 5.15f);
29+
30+
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone);
31+
}
32+
33+
@Test
34+
public void whenCheckingForElement_thenContains() throws Exception {
35+
List<String> list = Arrays.asList("1", "2", "3");
36+
37+
assertThat(list)
38+
.contains("1");
39+
}
40+
41+
@Test
42+
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
43+
List<String> list = Arrays.asList("1", "2", "3");
44+
45+
assertThat(list)
46+
.isNotEmpty()
47+
.contains("1")
48+
.startsWith("1")
49+
.doesNotContainNull()
50+
.containsSequence("2", "3");
51+
}
52+
53+
@Test
54+
public void whenCheckingRunnable_thenIsInterface() throws Exception {
55+
assertThat(Runnable.class).isInterface();
56+
}
57+
58+
@Test
59+
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
60+
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
61+
}
62+
63+
@Test
64+
public void whenComparingWithOffset_thenEquals() throws Exception {
65+
assertThat(5.1).isEqualTo(5, withPrecision(1d));
66+
}
67+
68+
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE
69+
@Test
70+
public void whenRunningAssertion_thenDescribed() throws Exception {
71+
Person person = new Person("Alex", 34);
72+
73+
assertThat(person.getAge())
74+
.as("%s's age should be equal to 100")
75+
.isEqualTo(100);
76+
}
77+
}

‎pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
<modules>
1111
<module>apache-fop</module>
12+
<module>assertj-introduction</module>
13+
1214
<module>core-java</module>
1315
<module>core-java-8</module>
1416
<module>gson</module>

0 commit comments

Comments
 (0)