From 6f11290160c6df54b83185432762324ad98da0ed Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Tue, 17 Aug 2021 12:12:55 +0100 Subject: [PATCH] [JAVA-6221] Test clean up --- .../baeldung/java9/time/TimeApiUnitTest.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java b/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java index c4e150c757..416a621286 100644 --- a/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java +++ b/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java @@ -1,12 +1,13 @@ package com.baeldung.java9.time; +import org.junit.Test; + import java.time.LocalDate; import java.util.Calendar; import java.util.Date; import java.util.List; -import static org.junit.Assert.assertEquals; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; public class TimeApiUnitTest { @@ -18,19 +19,18 @@ public class TimeApiUnitTest { Date endDate = endCalendar.getTime(); List dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate); - assertEquals(2, dates.size()); + + assertThat(dates).hasSize(2); Calendar calendar = Calendar.getInstance(); - Date date1 = calendar.getTime(); - assertEquals(date1.getDay(), dates.get(0).getDay()); - assertEquals(date1.getMonth(), dates.get(0).getMonth()); - assertEquals(date1.getYear(), dates.get(0).getYear()); + Date expectedDate1 = calendar.getTime(); + assertThat(dates.get(0)).isInSameDayAs(expectedDate1); + assertThatTimeFieldsAreZero(dates.get(0)); calendar.add(Calendar.DATE, 1); - Date date2 = calendar.getTime(); - assertEquals(date2.getDay(), dates.get(1).getDay()); - assertEquals(date2.getMonth(), dates.get(1).getMonth()); - assertEquals(date2.getYear(), dates.get(1).getYear()); + Date expectedDate2 = calendar.getTime(); + assertThat(dates.get(1)).isInSameDayAs(expectedDate2); + assertThatTimeFieldsAreZero(dates.get(1)); } @Test @@ -39,9 +39,8 @@ public class TimeApiUnitTest { LocalDate endDate = LocalDate.now().plusDays(2); List dates = TimeApi.getDatesBetweenUsingJava8(startDate, endDate); - assertEquals(dates.size(), 2); - assertEquals(dates.get(0), LocalDate.now()); - assertEquals(dates.get(1), LocalDate.now().plusDays(1)); + + assertThat(dates).containsExactly(LocalDate.now(), LocalDate.now().plusDays(1)); } @Test @@ -50,9 +49,15 @@ public class TimeApiUnitTest { LocalDate endDate = LocalDate.now().plusDays(2); List dates = TimeApi.getDatesBetweenUsingJava9(startDate, endDate); - assertEquals(dates.size(), 2); - assertEquals(dates.get(0), LocalDate.now()); - assertEquals(dates.get(1), LocalDate.now().plusDays(1)); + + assertThat(dates).containsExactly(LocalDate.now(), LocalDate.now().plusDays(1)); + } + + private static void assertThatTimeFieldsAreZero(Date date) { + assertThat(date).hasHourOfDay(0); + assertThat(date).hasMinute(0); + assertThat(date).hasSecond(0); + assertThat(date).hasMillisecond(0); } }