Skip to content

Commit 04aea56

Browse files
authored
Merge pull request eugenp#494 from eugenp/grzegorz-master
Add AssertJ Java8 examples
2 parents 4777996 + 6517c26 commit 04aea56

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
.isAfterOrEqualTo(LocalDate.of(1989, 7, 8));
47+
48+
assertThat(todayDate)
49+
.isAfter(LocalDate.of(1989, 7, 8))
50+
.isToday();
51+
}
52+
53+
@Test
54+
public void givenLocalDateTime_shouldAssert() throws Exception {
55+
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
56+
57+
assertThat(givenLocalDate)
58+
.isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
59+
}
60+
61+
@Test
62+
public void givenLocalTime_shouldAssert() throws Exception {
63+
final LocalTime givenLocalTime = LocalTime.of(12, 15);
64+
65+
assertThat(givenLocalTime)
66+
.isAfter(LocalTime.of(1, 0))
67+
.hasSameHourAs(LocalTime.of(12, 0));
68+
}
69+
70+
@Test
71+
public void givenList_shouldAssertFlatExtracting() throws Exception {
72+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
73+
74+
assertThat(givenList)
75+
.flatExtracting(LocalDate::getYear)
76+
.contains(2015);
77+
}
78+
79+
@Test
80+
public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception {
81+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
82+
83+
assertThat(givenList)
84+
.flatExtracting(LocalDate::isLeapYear)
85+
.contains(true);
86+
}
87+
88+
@Test
89+
public void givenList_shouldAssertFlatExtractingClass() throws Exception {
90+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
91+
92+
assertThat(givenList)
93+
.flatExtracting(Object::getClass)
94+
.contains(LocalDate.class);
95+
}
96+
97+
@Test
98+
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
99+
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
100+
101+
assertThat(givenList)
102+
.flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth)
103+
.contains(2015, 6);
104+
}
105+
106+
@Test
107+
public void givenString_shouldSatisfy() throws Exception {
108+
final String givenString = "someString";
109+
110+
assertThat(givenString)
111+
.satisfies(s -> {
112+
assertThat(s).isNotEmpty();
113+
assertThat(s).hasSize(10);
114+
});
115+
}
116+
117+
@Test
118+
public void givenString_shouldMatch() throws Exception {
119+
final String emptyString = "";
120+
121+
assertThat(emptyString)
122+
.matches(String::isEmpty);
123+
}
124+
125+
@Test
126+
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
127+
final List<String> givenList = Arrays.asList("");
128+
129+
assertThat(givenList)
130+
.hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
131+
}
132+
}

0 commit comments

Comments
 (0)