Skip to content

Commit a9a5a3d

Browse files
author
Grzegorz Piwowarek
committed
Add examples
1 parent 40ed303 commit a9a5a3d

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎assertj/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@
99
<version>1.0.0-SNAPSHOT</version>
1010

1111
<dependencies>
12+
<dependency>
13+
<groupId>com.google.guava</groupId>
14+
<artifactId>guava</artifactId>
15+
<version>19.0</version>
16+
</dependency>
1217
<dependency>
1318
<groupId>junit</groupId>
1419
<artifactId>junit</artifactId>
1520
<version>4.12</version>
21+
<scope>test</scope>
1622
</dependency>
1723
<dependency>
1824
<groupId>org.assertj</groupId>
1925
<artifactId>assertj-core</artifactId>
2026
<version>3.4.1</version>
2127
<scope>test</scope>
2228
</dependency>
29+
<dependency>
30+
<groupId>org.assertj</groupId>
31+
<artifactId>assertj-guava</artifactId>
32+
<version>3.0.0</version>
33+
</dependency>
2334
</dependencies>
2435

2536
<build>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.assertj.introduction;
2+
3+
import com.google.common.base.Optional;
4+
import com.google.common.collect.HashBasedTable;
5+
import com.google.common.collect.ImmutableMap;
6+
import com.google.common.collect.Multimaps;
7+
import com.google.common.collect.Range;
8+
import com.google.common.collect.SetMultimap;
9+
import com.google.common.collect.TreeRangeMap;
10+
import com.google.common.io.Files;
11+
import org.assertj.guava.data.MapEntry;
12+
import org.junit.Test;
13+
14+
import java.io.File;
15+
16+
import static org.assertj.guava.api.Assertions.assertThat;
17+
import static org.assertj.guava.api.Assertions.entry;
18+
19+
public class AssertJGuavaTest {
20+
21+
@Test
22+
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
23+
final File temp = File.createTempFile("bael", "dung");
24+
final File temp2 = File.createTempFile("bael", "dung2");
25+
26+
assertThat(Files.asByteSource(temp))
27+
.hasSize(0)
28+
.hasSameContentAs(Files.asByteSource(temp2));
29+
}
30+
31+
@Test
32+
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
33+
final SetMultimap<Object, Object> mmap = Multimaps.forMap(ImmutableMap.of(1, "one"));
34+
35+
assertThat(mmap)
36+
.hasSize(1)
37+
.containsKeys(1)
38+
.contains(entry(1, "one"));
39+
}
40+
41+
@Test
42+
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
43+
final Optional<String> something = Optional.of("something");
44+
45+
assertThat(something)
46+
.isPresent()
47+
.extractingValue()
48+
.isEqualTo("something");
49+
}
50+
51+
@Test
52+
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
53+
final Range<String> range = Range.openClosed("a", "g");
54+
55+
assertThat(range)
56+
.hasOpenedLowerBound()
57+
.isNotEmpty()
58+
.hasClosedUpperBound()
59+
.contains("b");
60+
}
61+
62+
@Test
63+
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception {
64+
final TreeRangeMap<Integer, String> map = TreeRangeMap.create();
65+
66+
map.put(Range.closed(0, 60), "F");
67+
map.put(Range.closed(61, 70), "D");
68+
69+
assertThat(map)
70+
.isNotEmpty()
71+
.containsKeys(0)
72+
.contains(MapEntry.entry(34, "F"));
73+
}
74+
75+
@Test
76+
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception {
77+
final HashBasedTable<Integer, String, String> table = HashBasedTable.create(2, 2);
78+
79+
table.put(1, "A", "PRESENT");
80+
table.put(1, "B", "ABSENT");
81+
82+
assertThat(table)
83+
.hasRowCount(1)
84+
.containsValues("ABSENT")
85+
.containsCell(1, "B", "ABSENT");
86+
}
87+
88+
89+
90+
91+
}

0 commit comments

Comments
 (0)