From 42126790909fdebc24b21786255132cd7aaf1bfd Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 5 Oct 2025 01:07:13 +0200 Subject: [PATCH] Apply a few assertion best practices --- .../examples/DateAssertionsExamples.java | 4 +-- .../examples/NumberAssertionsExamples.java | 26 +++++++++---------- .../examples/SoftAssertionsExamples.java | 22 ++++++++-------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/assertions-examples/src/test/java/org/assertj/examples/DateAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/DateAssertionsExamples.java index 8afcc51..392bc6b 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/DateAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/DateAssertionsExamples.java @@ -104,7 +104,7 @@ public void date_assertions_comparison_with_precision_level() { Date date5 = parseDatetimeWithMs("2003-01-01T05:55:55.555"); assertThat(date1) - .isEqualToIgnoringMillis(date2) + .isCloseTo(date2, 1000L) .isInSameSecondAs(date2) .isEqualToIgnoringSeconds(date3) .isInSameMinuteAs(date3) @@ -114,7 +114,7 @@ public void date_assertions_comparison_with_precision_level() { .isInSameDayAs(date5); try { - assertThat(date1).isEqualToIgnoringMillis(date3); + assertThat(date1).isCloseTo(date3, 1000L); } catch (AssertionError e) { logAssertionErrorMessage("isEqualToIgnoringMillis", e); } diff --git a/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java index 1dc72a9..79a16f7 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/NumberAssertionsExamples.java @@ -129,10 +129,10 @@ public void big_decimals_assertions_examples() { @Test public void number_assertions_with_offset_examples() { - assertThat(8.1).isEqualTo(8.0, offset(0.1)); - assertThat(8.1f).isEqualTo(8.2f, offset(0.1f)); + assertThat(8.1).isCloseTo(8.0, offset(0.1)); + assertThat(8.1f).isCloseTo(8.2f, offset(0.1f)); try { - assertThat(8.1f).isEqualTo(8.0f, offset(0.1f)); + assertThat(8.1f).isCloseTo(8.0f, offset(0.1f)); } catch (AssertionError e) { logAssertionErrorMessage("float isEqualTo with offset", e); } @@ -198,12 +198,12 @@ public void number_assertions_with_offset_examples() { assertThat(8.1f) .isEqualTo(8.0f, within(0.2f)) .isEqualTo(8.0f, offset(0.2f)) - // alias of within - .isEqualTo(8.0f, byLessThan(0.2f)); assertThat(0.1f) + .isCloseTo(8.0f, byLessThan(0.2f)); + assertThat(0.1f) // strict // assertions succeed when the difference == offset value ... .isCloseTo(0.0f, within(0.1f)) - .isEqualTo(0.0f, within(0.1f)); + .isCloseTo(0.0f, within(0.1f)); assertThat(8.1f) .isNotCloseTo(8.0f, byLessThan(0.01f)) @@ -215,23 +215,23 @@ public void number_assertions_with_offset_examples() { assertThat(8.1f) .isEqualTo(8.0f, within(0.2f)) .isEqualTo(8.0f, offset(0.2f)) - // alias of within - .isEqualTo(8.0f, byLessThan(0.2f)); assertThat(0.1f) + .isCloseTo(8.0f, byLessThan(0.2f)); + assertThat(0.1f) // strict // assertions succeed when the difference == offset value ... .isEqualTo(0.0f, within(0.1f)) - .isEqualTo(0.0f, offset(0.1f)); + .isCloseTo(0.0f, offset(0.1f)); assertThat(8.1f) // ... except when using byLessThan which implies a strict comparison // assertThat(0.1f).isEqualTo(0.0f, byLessThan(0.1f)); // strict => fail .isEqualTo(8.0f, within(0.2f)) .isEqualTo(8.0f, offset(0.2f)) - // alias of within - .isEqualTo(8.0f, byLessThan(0.2f)); assertThat(0.1f) + .isCloseTo(8.0f, byLessThan(0.2f)); + assertThat(0.1f) // strict // assertions succeed when the difference == offset value ... .isEqualTo(0.0f, within(0.1f)) - .isEqualTo(0.0f, offset(0.1f)); + .isCloseTo(0.0f, offset(0.1f)); assertThat(5) .isCloseTo(7, within(3)) @@ -298,7 +298,7 @@ public void isNotCloseTo_examples() { @Test public void number_assertions_with_binary_representation_examples() { - assertThat(1).inBinary().isEqualTo(1); + assertThat(1).inBinary().isOne(); try { assertThat(1).inBinary().isEqualTo(2); } catch (AssertionError e) { diff --git a/assertions-examples/src/test/java/org/assertj/examples/SoftAssertionsExamples.java b/assertions-examples/src/test/java/org/assertj/examples/SoftAssertionsExamples.java index 973eda3..fb46227 100644 --- a/assertions-examples/src/test/java/org/assertj/examples/SoftAssertionsExamples.java +++ b/assertions-examples/src/test/java/org/assertj/examples/SoftAssertionsExamples.java @@ -81,17 +81,17 @@ public void build_soft_assertions_errors_report_example() { // let the assertions fail catchThrowable(() -> softly.assertAll()); // check the built report - assertThat(reportBuilder.toString()).isEqualTo(format("Assertions report:%n" + - "------------------%n" + - "[check Frodo's race] %n" + - "expected: \"Orc\"%n" + - "but was : \"Hobbit\"%n" + - "------------------%n" + - "[check Aragorn's age] %n" + - "Expecting:%n" + - " 87%n" + - "to be greater than:%n" + - " 500%n%n")); + assertThat(reportBuilder).hasToString(format("Assertions report:%n" + + "------------------%n" + + "[check Frodo's race] %n" + + "expected: \"Orc\"%n" + + "but was : \"Hobbit\"%n" + + "------------------%n" + + "[check Aragorn's age] %n" + + "Expecting:%n" + + " 87%n" + + "to be greater than:%n" + + " 500%n%n")); } @Test