From ee3af0efe04212af23d21982fc729015c7cbacc2 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 12 Jul 2016 14:21:44 +0300 Subject: [PATCH 1/5] Add AssertJ Java8 examples --- .../introduction/AssertJJava8Test.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java new file mode 100644 index 0000000000..2a35f629fe --- /dev/null +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java @@ -0,0 +1,112 @@ +package com.baeldung.assertj.introduction; + +import org.junit.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; + +import static java.time.LocalDate.ofYearDay; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class AssertJJava8Test { + + @Test + public void givenOptional_shouldAssert() throws Exception { + final Optional givenOptional = Optional.of("something"); + + assertThat(givenOptional) + .isPresent() + .hasValue("something"); + } + + @Test + public void givenPredicate_shouldAssert() throws Exception { + final Predicate predicate = s -> s.length() > 4; + + assertThat(predicate) + .accepts("aaaaa", "bbbbb") + .rejects("a", "b") + .acceptsAll(asList("aaaaa", "bbbbb")) + .rejectsAll(asList("a", "b")); + } + + @Test + public void givenLocalDate_shouldAssert() throws Exception { + final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8); + final LocalDate todayDate = LocalDate.now(); + + assertThat(givenLocalDate) + .isBefore(LocalDate.of(2020, 7, 8)); + + assertThat(todayDate) + .isAfter(LocalDate.of(1989, 7, 8)) + .isToday(); + } + + @Test + public void givenLocalDateTime_shouldAssert() throws Exception { + final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0); + + assertThat(givenLocalDate) + .isBefore(LocalDateTime.of(2020, 7, 8, 11, 2)); + } + + @Test + public void givenLocalTime_shouldAssert() throws Exception { + final LocalTime givenLocalTime = LocalTime.of(12, 15); + + assertThat(givenLocalTime) + .hasSameHourAs(LocalTime.of(12, 0)); + } + + @Test + public void givenList_shouldAssertFlatExtracting() throws Exception { + final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); + + assertThat(givenList) + .flatExtracting(LocalDate::getYear) + .contains(2015); + } + + @Test + public void givenList_shouldAssertMultipleFlatExtracting() throws Exception { + final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); + + assertThat(givenList) + .flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth) + .contains(2015, 6); + } + + @Test + public void givenString_shouldSatisfy() throws Exception { + final String givenString = "someString"; + + assertThat(givenString) + .satisfies(s -> { + assertThat(s).isNotEmpty(); + assertThat(s).hasSize(10); + }); + } + + @Test + public void givenString_shouldMatch() throws Exception { + final String givenString = ""; + + assertThat(givenString) + .matches(String::isEmpty); + } + + @Test + public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception { + final List givenList = Arrays.asList(""); + + assertThat(givenList) + .hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty()); + } +} From 0334f06ca5cee352528d72ebf28e8122ee24c0fa Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 12 Jul 2016 20:51:42 +0300 Subject: [PATCH 2/5] Refactor --- .../com/baeldung/assertj/introduction/AssertJJava8Test.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java index 2a35f629fe..d49f7ab42d 100644 --- a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java @@ -42,7 +42,8 @@ public class AssertJJava8Test { final LocalDate todayDate = LocalDate.now(); assertThat(givenLocalDate) - .isBefore(LocalDate.of(2020, 7, 8)); + .isBefore(LocalDate.of(2020, 7, 8)) + .isAfterOrEqualTo(LocalDate.of(1989, 7, 8)); assertThat(todayDate) .isAfter(LocalDate.of(1989, 7, 8)) From 060d1594aa33ddc4a7090fe563be5621d725473e Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 12 Jul 2016 20:58:01 +0300 Subject: [PATCH 3/5] Formatting --- .../introduction/AssertJJava8Test.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java index d49f7ab42d..8700b38ca8 100644 --- a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java @@ -21,8 +21,8 @@ public class AssertJJava8Test { final Optional givenOptional = Optional.of("something"); assertThat(givenOptional) - .isPresent() - .hasValue("something"); + .isPresent() + .hasValue("something"); } @Test @@ -30,10 +30,10 @@ public class AssertJJava8Test { final Predicate predicate = s -> s.length() > 4; assertThat(predicate) - .accepts("aaaaa", "bbbbb") - .rejects("a", "b") - .acceptsAll(asList("aaaaa", "bbbbb")) - .rejectsAll(asList("a", "b")); + .accepts("aaaaa", "bbbbb") + .rejects("a", "b") + .acceptsAll(asList("aaaaa", "bbbbb")) + .rejectsAll(asList("a", "b")); } @Test @@ -42,12 +42,12 @@ public class AssertJJava8Test { final LocalDate todayDate = LocalDate.now(); assertThat(givenLocalDate) - .isBefore(LocalDate.of(2020, 7, 8)) - .isAfterOrEqualTo(LocalDate.of(1989, 7, 8)); + .isBefore(LocalDate.of(2020, 7, 8)) + .isAfterOrEqualTo(LocalDate.of(1989, 7, 8)); assertThat(todayDate) - .isAfter(LocalDate.of(1989, 7, 8)) - .isToday(); + .isAfter(LocalDate.of(1989, 7, 8)) + .isToday(); } @Test @@ -55,7 +55,7 @@ public class AssertJJava8Test { final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0); assertThat(givenLocalDate) - .isBefore(LocalDateTime.of(2020, 7, 8, 11, 2)); + .isBefore(LocalDateTime.of(2020, 7, 8, 11, 2)); } @Test @@ -63,7 +63,7 @@ public class AssertJJava8Test { final LocalTime givenLocalTime = LocalTime.of(12, 15); assertThat(givenLocalTime) - .hasSameHourAs(LocalTime.of(12, 0)); + .hasSameHourAs(LocalTime.of(12, 0)); } @Test @@ -71,8 +71,8 @@ public class AssertJJava8Test { final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); assertThat(givenList) - .flatExtracting(LocalDate::getYear) - .contains(2015); + .flatExtracting(LocalDate::getYear) + .contains(2015); } @Test @@ -80,8 +80,8 @@ public class AssertJJava8Test { final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); assertThat(givenList) - .flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth) - .contains(2015, 6); + .flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth) + .contains(2015, 6); } @Test @@ -89,10 +89,10 @@ public class AssertJJava8Test { final String givenString = "someString"; assertThat(givenString) - .satisfies(s -> { - assertThat(s).isNotEmpty(); - assertThat(s).hasSize(10); - }); + .satisfies(s -> { + assertThat(s).isNotEmpty(); + assertThat(s).hasSize(10); + }); } @Test @@ -100,7 +100,7 @@ public class AssertJJava8Test { final String givenString = ""; assertThat(givenString) - .matches(String::isEmpty); + .matches(String::isEmpty); } @Test @@ -108,6 +108,6 @@ public class AssertJJava8Test { final List givenList = Arrays.asList(""); assertThat(givenList) - .hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty()); + .hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty()); } } From 79e2c3ed493d7e0c74400e0e772414b24f6b3dba Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 12 Jul 2016 21:14:57 +0300 Subject: [PATCH 4/5] Add additional examples --- .../introduction/AssertJJava8Test.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java index 8700b38ca8..4671ee82bb 100644 --- a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java @@ -63,6 +63,7 @@ public class AssertJJava8Test { final LocalTime givenLocalTime = LocalTime.of(12, 15); assertThat(givenLocalTime) + .isAfter(LocalTime.of(1, 0)) .hasSameHourAs(LocalTime.of(12, 0)); } @@ -75,6 +76,24 @@ public class AssertJJava8Test { .contains(2015); } + @Test + public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception { + final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); + + assertThat(givenList) + .flatExtracting(LocalDate::isLeapYear) + .contains(true); + } + + @Test + public void givenList_shouldAssertFlatExtractingClass() throws Exception { + final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); + + assertThat(givenList) + .flatExtracting(Object::getClass) + .contains(LocalDate.class); + } + @Test public void givenList_shouldAssertMultipleFlatExtracting() throws Exception { final List givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6)); From 6517c26dd606aa3a5bcb4707e355a92eeabe6766 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 12 Jul 2016 21:33:20 +0300 Subject: [PATCH 5/5] Refactor variable's name --- .../com/baeldung/assertj/introduction/AssertJJava8Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java index 4671ee82bb..0cdbd0f7ea 100644 --- a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJJava8Test.java @@ -116,9 +116,9 @@ public class AssertJJava8Test { @Test public void givenString_shouldMatch() throws Exception { - final String givenString = ""; + final String emptyString = ""; - assertThat(givenString) + assertThat(emptyString) .matches(String::isEmpty); }