Table of Contents
ToggleAsserting LocalDateTimes with AssertJ
介紹 matches 、 satisfies 驗證是否符合條件,類似的流暢或鍊式寫法,語法跟自然語言相近,對於編寫測試時力求容易閱讀及維護這之上提供了相當大的改進, Assert LocalDateTimes in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- spring_boot_168
| +- test
| +- assertj
| +- localdatetime
| +- AssertingLocalDateTimesTest.java
單元測試
Assertions LocalDateTimes in Java 斷言日期時間的主要目的是取得日期時間以進行斷言。
matches
Assertions LocalDateTimes in Java 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void matches() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2022);
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.OCTOBER);
}
2023-10-31T05:06:07
2023-10-31T05:06:07
matchesWithDescription
Assertions LocalDateTimes in Java 驗證是否符合條件,自訂描述訊息,若不成立,則會拋出 AssertionError 。
@Test
public void matchesWithDescription() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2022, "yearGreaterThan");
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.OCTOBER, "monthEqual");
}
2023-10-31T05:06:07
2023-10-31T05:06:07
matchesThrowError
Assertions LocalDateTimes in Java 驗證是否符合條件,自訂描述訊息,當不成立時,驗證拋出 AssertionError 。
@Test
public void matchesThrowError() {
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2024, "yearGreaterThan");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.NOVEMBER, "monthEqual");
}).isInstanceOf(AssertionError.class);
}
2023-10-31T05:06:07
2023-10-31T05:06:07
satisfies
Asserting LocalDateTimes with Examples 驗證是否符合條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfies() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfies(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 2), within(5, ChronoUnit.SECONDS));
});
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfies(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2023, 10, 31, 5, 6, 7), LocalDateTime.of(2023, 10, 31, 6, 6, 7));
});
}
2023-10-31T05:06:07
2023-10-31T05:06:07
satisfiesAnyOf
Asserting LocalDateTimes with Examples 驗證是否符合任一條件,若不成立,則會拋出 AssertionError 。
@Test
public void satisfiesAnyOf() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfiesAnyOf(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 2), within(5, ChronoUnit.SECONDS));
});
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfiesAnyOf(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2000, 10, 31, 5, 6, 7), LocalDateTime.of(2000, 10, 31, 6, 6, 7));
});
}
2023-10-31T05:06:07
2023-10-31T05:06:07
satisfiesThrowError
Asserting LocalDateTimes with Examples 驗證是否符合條件,當不成立時,驗證拋出 AssertionError 。
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfies(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 22), within(5, ChronoUnit.SECONDS));
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfies(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2023, 10, 31, 5, 6, 8), LocalDateTime.of(2023, 10, 31, 6, 6, 7));
});
}).isInstanceOf(AssertionError.class);
}
2023-10-31T05:06:07
2023-10-31T05:06:07
AssertingLocalDateTimesTest.java
Asserting LocalDateTimes with Examples 新增單元測試,驗證是否符合預期。
package org.ruoxue.spring_boot_168.test.assertj.localdatetime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.within;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
public class AssertingLocalDateTimesTest {
@Test
public void matches() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2022);
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.OCTOBER);
}
@Test
public void matchesWithDescription() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2022, "yearGreaterThan");
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.OCTOBER, "monthEqual");
}
@Test
public void matchesThrowError() {
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).matches(l -> l.getYear() > 2024, "yearGreaterThan");
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).matches(l -> l.getMonth() == Month.NOVEMBER, "monthEqual");
}).isInstanceOf(AssertionError.class);
}
@Test
public void satisfies() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfies(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 2), within(5, ChronoUnit.SECONDS));
});
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfies(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2023, 10, 31, 5, 6, 7), LocalDateTime.of(2023, 10, 31, 6, 6, 7));
});
}
@Test
public void satisfiesAnyOf() {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfiesAnyOf(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 2), within(5, ChronoUnit.SECONDS));
});
value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfiesAnyOf(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2000, 10, 31, 5, 6, 7), LocalDateTime.of(2000, 10, 31, 6, 6, 7));
});
}
@Test
public void satisfiesThrowError() {
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.parse("2023-10-31T05:06:07");
System.out.println(value);
assertThat(value).satisfies(l -> {
assertThat(l).isNotNull();
assertThat(l).isCloseTo(LocalDateTime.of(2023, 10, 31, 5, 6, 22), within(5, ChronoUnit.SECONDS));
});
}).isInstanceOf(AssertionError.class);
assertThatCode(() -> {
LocalDateTime value = LocalDateTime.of(2023, 10, 31, 5, 6, 7);
System.out.println(value);
assertThat(value).satisfies(p -> {
assertThat(p).isBefore(LocalDateTime.of(2023, 10, 31, 5, 6, 12));
}, p -> {
assertThat(p).isIn(LocalDateTime.of(2023, 10, 31, 5, 6, 8), LocalDateTime.of(2023, 10, 31, 6, 6, 7));
});
}).isInstanceOf(AssertionError.class);
}
}
心得分享
Testing Java LocalDateTimes with AssertJ 快速檢查物件是否滿足所有提供的斷言,搭配使用 Predicate 與 ThrowingConsumer 等接口,根據 lambda 表達式調用,驗證是否成立,若不成立,則會拋出 AssertionError, Asserting LocalDateTimes with Examples 介紹 matches 、 satisfies 等方法,提供範例參考。
原碼下載