Skip to content

Commit a871f25

Browse files
author
Grzegorz Piwowarek
committed
Add first set of assertions
1 parent 83df968 commit a871f25

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

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: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
package com.baeldung.assertj;
22

3+
import com.baeldung.assertj.domain.Dog;
34
import org.junit.Test;
45

6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.NoSuchElementException;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.withPrecision;
12+
513
public class AssertJDemoTest {
614

715
@Test
8-
public void shouldTest() throws Exception {
16+
public void whenComparingReferences_thenNotEqual() throws Exception {
17+
Dog fido = new Dog("Fido", 5.15f);
18+
Dog fidosClone = new Dog("Fido", 5.15f);
19+
20+
assertThat(fido).isNotEqualTo(fidosClone);
21+
}
22+
23+
@Test
24+
public void whenComparingFields_thenEqual() throws Exception {
25+
Dog fido = new Dog("Fido", 5.15f);
26+
Dog fidosClone = new Dog("Fido", 5.15f);
27+
28+
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone);
29+
}
30+
31+
@Test
32+
public void whenCheckingForElement_thenContains() throws Exception {
33+
List<String> list = Arrays.asList("1", "2", "3");
34+
35+
assertThat(list)
36+
.contains("1");
37+
}
38+
39+
@Test
40+
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
41+
List<String> list = Arrays.asList("1", "2", "3");
42+
43+
assertThat(list)
44+
.isNotEmpty()
45+
.contains("1")
46+
.startsWith("1")
47+
.doesNotContainNull()
48+
.containsSequence("2", "3");
49+
}
50+
51+
@Test
52+
public void whenCheckingRunnable_thenIsInterface() throws Exception {
53+
assertThat(Runnable.class).isInterface();
54+
}
55+
56+
@Test
57+
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
58+
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
59+
}
60+
61+
@Test
62+
public void whenComparingWithOffset_thenEquals() throws Exception {
63+
assertThat(5.1).isEqualTo(5, withPrecision(1d));
964
}
1065
}

0 commit comments

Comments
 (0)