Skip to content

Commit ee3af0e

Browse files
author
Grzegorz Piwowarek
committed
Add AssertJ Java8 examples
1 parent afb6a25 commit ee3af0e

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.baeldung.assertj.introduction;
2+
3+
import org.junit.Test;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
import java.time.LocalTime;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.function.Predicate;
12+
13+
import static java.time.LocalDate.ofYearDay;
14+
import static java.util.Arrays.asList;
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
public class AssertJJava8Test {
18+
19+
@Test
20+
public void givenOptional_shouldAssert() throws Exception {
21+
final Optional<String> givenOptional = Optional.of("something");
22+
23+
assertThat(givenOptional)
24+
.isPresent()
25+
.hasValue("something");
26+
}
27+
28+
@Test
29+
public void givenPredicate_shouldAssert() throws Exception {
30+
final Predicate<String> predicate = s -> s.length() > 4;
31+
32+
assertThat(predicate)
33+
.accepts("aaaaa", "bbbbb")
34+
.rejects("a", "b")
35+
.acceptsAll(asList("aaaaa", "bbbbb"))
36+
.rejectsAll(asList("a", "b"));
37+
}
38+
39+
@Test
40+
public void givenLocalDate_shouldAssert() throws Exception {
41+
final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
42+
final LocalDate todayDate = LocalDate.now();
43+
44+
assertThat(givenLocalDate)
45+
.isBefore(LocalDate.of(2020, 7, 8));
46+
47+
assertThat(todayDate)
48+
.isAfter(LocalDate.of(1989, 7, 8))
49+
.isToday();
50+
}
51+
52+
@Test
53+
public void givenLocalDateTime_shouldAssert() throws Exception {
54+
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
55+
56+
assertThat(givenLocalDate)
57+
.isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
58+
}
59+
60+
@Test
61+
public void givenLocalTime_shouldAssert() throws Exception {
62+
final LocalTime givenLocalTime = LocalTime.of(12, 15);
63+
64+
assertThat(givenLocalTime)
65+
.hasSameHourAs(LocalTime.of(12, 0));
66+
}
67+
68+
@Test
69+
public void givenList_shouldAssertFlatExtracting() throws Exception {
70+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
71+
72+
assertThat(givenList)
73+
.flatExtracting(LocalDate::getYear)
74+
.contains(2015);
75+
}
76+
77+
@Test
78+
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
79+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
80+
81+
assertThat(givenList)
82+
.flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth)
83+
.contains(2015, 6);
84+
}
85+
86+
@Test
87+
public void givenString_shouldSatisfy() throws Exception {
88+
final String givenString = "someString";
89+
90+
assertThat(givenString)
91+
.satisfies(s -> {
92+
assertThat(s).isNotEmpty();
93+
assertThat(s).hasSize(10);
94+
});
95+
}
96+
97+
@Test
98+
public void givenString_shouldMatch() throws Exception {
99+
final String givenString = "";
100+
101+
assertThat(givenString)
102+
.matches(String::isEmpty);
103+
}
104+
105+
@Test
106+
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
107+
final List<String> givenList = Arrays.asList("");
108+
109+
assertThat(givenList)
110+
.hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
111+
}
112+
}

0 commit comments

Comments
 (0)