Skip to content

Commit 024b4f8

Browse files
author
Grzegorz Piwowarek
committed
Add additional AssertJ examples
1 parent b3e9b08 commit 024b4f8

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ target/
2727

2828
spring-openid/src/main/resources/application.properties
2929
.recommenders/
30+

assertj/src/test/java/com/baeldung/assertj/introduction/AssertJDemoTest.java renamed to assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
import com.baeldung.assertj.introduction.domain.Dog;
44
import com.baeldung.assertj.introduction.domain.Person;
5+
import org.assertj.core.util.Maps;
56
import org.junit.Ignore;
67
import org.junit.Test;
78

9+
import java.io.ByteArrayInputStream;
10+
import java.io.File;
11+
import java.io.InputStream;
812
import java.util.Arrays;
913
import java.util.List;
14+
import java.util.Map;
1015
import java.util.NoSuchElementException;
1116

1217
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.entry;
1319
import static org.assertj.core.api.Assertions.withPrecision;
1420

15-
public class AssertJDemoTest {
21+
public class AssertJCoreTest {
1622

1723
@Test
1824
public void whenComparingReferences_thenNotEqual() throws Exception {
@@ -42,6 +48,10 @@ public void whenCheckingForElement_thenContains() throws Exception {
4248
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
4349
List<String> list = Arrays.asList("1", "2", "3");
4450

51+
assertThat(list).isNotEmpty();
52+
assertThat(list).startsWith("1");
53+
assertThat(list).doesNotContainNull();
54+
4555
assertThat(list)
4656
.isNotEmpty()
4757
.contains("1")
@@ -55,6 +65,17 @@ public void whenCheckingRunnable_thenIsInterface() throws Exception {
5565
assertThat(Runnable.class).isInterface();
5666
}
5767

68+
@Test
69+
public void whenCheckingCharacter_thenIsUnicode() throws Exception {
70+
char someCharacter = 'c';
71+
72+
assertThat(someCharacter)
73+
.isNotEqualTo('a')
74+
.inUnicode()
75+
.isGreaterThanOrEqualTo('b')
76+
.isLowerCase();
77+
}
78+
5879
@Test
5980
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
6081
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
@@ -65,6 +86,51 @@ public void whenComparingWithOffset_thenEquals() throws Exception {
6586
assertThat(5.1).isEqualTo(5, withPrecision(1d));
6687
}
6788

89+
@Test
90+
public void whenCheckingString_then() throws Exception {
91+
assertThat("".isEmpty()).isTrue();
92+
}
93+
94+
@Test
95+
public void whenCheckingFile_then() throws Exception {
96+
final File someFile = File.createTempFile("aaa", "bbb");
97+
someFile.deleteOnExit();
98+
99+
assertThat(someFile)
100+
.exists()
101+
.isFile()
102+
.canRead()
103+
.canWrite();
104+
}
105+
106+
@Test
107+
public void whenCheckingIS_then() throws Exception {
108+
InputStream given = new ByteArrayInputStream("foo".getBytes());
109+
InputStream expected = new ByteArrayInputStream("foo".getBytes());
110+
111+
assertThat(given).hasSameContentAs(expected);
112+
}
113+
114+
@Test
115+
public void whenGivenMap_then() throws Exception {
116+
Map<Integer, String> map = Maps.newHashMap(2, "a");
117+
118+
assertThat(map)
119+
.isNotEmpty()
120+
.containsKey(2)
121+
.doesNotContainKeys(10)
122+
.contains(entry(2, "a"));
123+
}
124+
125+
@Test
126+
public void whenGivenException_then() throws Exception {
127+
Exception ex = new Exception("abc");
128+
129+
assertThat(ex)
130+
.hasNoCause()
131+
.hasMessageEndingWith("c");
132+
}
133+
68134
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE
69135
@Test
70136
public void whenRunningAssertion_thenDescribed() throws Exception {

0 commit comments

Comments
 (0)