Skip to content

Backport: Add withFormatter() for Duration assertions#4024

Merged
scordio merged 4 commits into
assertj:3.xfrom
dajoropo:#4017_cherry-pick_#3976
Nov 22, 2025
Merged

Backport: Add withFormatter() for Duration assertions#4024
scordio merged 4 commits into
assertj:3.xfrom
dajoropo:#4017_cherry-pick_#3976

Conversation

@dajoropo

@dajoropo dajoropo commented Nov 22, 2025

Copy link
Copy Markdown
Contributor

@scordio scordio changed the title #4017 cherry pick #3976 Add withFormatter() for Duration assertions (backport) Nov 22, 2025
@dajoropo dajoropo force-pushed the #4017_cherry-pick_#3976 branch from 798c34d to 7d89a07 Compare November 22, 2025 11:03
@scordio scordio added the event: Hack.Commit.Push A topic worked on during a Hack.Commit.Push event label Nov 22, 2025
@scordio scordio requested a review from Copilot November 22, 2025 12:54
@scordio scordio added this to the 3.28.0 milestone Nov 22, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a withFormatter() method to Duration assertions, allowing developers to customize how Duration objects are formatted in assertion failure messages. This is a backport addressing issue #4017.

  • Introduces withFormatter(Function<Duration, String>) method to AbstractDurationAssert
  • Changes visibility of StandardRepresentation.toStringOf(Duration) from private to protected to enable custom formatting
  • Adds comprehensive test coverage for the new formatter functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
AbstractDurationAssert.java Adds the withFormatter() method with full JavaDoc documentation and validation
StandardRepresentation.java Changes visibility of toStringOf(Duration) from private to protected to support custom representations
DurationAssert_withFormatter_Test.java Adds tests for null validation and formatter functionality
DurationAssert_isCloseTo_Test.java Fixes typo in test annotation: "close" to "be close"
Comments suppressed due to low confidence (3)

assertj-core/src/main/java/org/assertj/core/api/AbstractDurationAssert.java:1

  • Syntax error: semicolon after .withFormatter(formatter) on line 321 should be removed. The correct syntax should chain the method calls without an intermediate semicolon.
/*

assertj-core/src/main/java/org/assertj/core/api/AbstractDurationAssert.java:1

  • The JavaDoc example is incomplete and misleading. The comment states 'this assertion succeeds' but the code has a placeholder . in hasDays(.) on line 328 and a semicolon after line 327. This should either show a complete, valid example with an actual value (e.g., .hasDays(3)) or be removed if it doesn't add value beyond the first example.
/*

assertj-core/src/main/java/org/assertj/core/presentation/StandardRepresentation.java:211

  • Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
    Method StandardRepresentation.toStringOf(..) could be confused with overloaded method toStringOf, since dispatch depends on static types.
  public String toStringOf(Object object) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@scordio scordio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dajoropo! Inspired by the Copilot review, I found only two cosmetic issues (which are also in the original changes).

Comment thread assertj-core/src/main/java/org/assertj/core/api/AbstractDurationAssert.java Outdated
Comment thread assertj-core/src/main/java/org/assertj/core/api/AbstractDurationAssert.java Outdated
…onAssert.java

Co-authored-by: Stefano Cordio <stefano.cordio@gmail.com>
@scordio scordio self-assigned this Nov 22, 2025
…onAssert.java

Co-authored-by: Stefano Cordio <stefano.cordio@gmail.com>
@scordio scordio changed the title Add withFormatter() for Duration assertions (backport) Backport: Add withFormatter() for Duration assertions Nov 22, 2025
@scordio scordio merged commit 1fb51d8 into assertj:3.x Nov 22, 2025
16 of 17 checks passed
@scordio

scordio commented Nov 22, 2025

Copy link
Copy Markdown
Member

Congratulations on your first contribution, @dajoropo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event: Hack.Commit.Push A topic worked on during a Hack.Commit.Push event

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants