Skip to content

Back org.testng.Assert with AssertJ for behaviour-identical assertions#20

Merged
krmahadevan merged 3 commits into
mainfrom
port-assertj-delegation
Jun 30, 2026
Merged

Back org.testng.Assert with AssertJ for behaviour-identical assertions#20
krmahadevan merged 3 commits into
mainfrom
port-assertj-delegation

Conversation

@juherr

@juherr juherr commented Jun 28, 2026

Copy link
Copy Markdown
Member

Summary

Ports the AssertJ-backed assertion implementation from
testng-team/testng#3278 into this standalone
org.testng:testng-asserts artifact. Once released, the parent PR can drop these
testng-asserts/src/main changes and simply depend on this artifact.

Only assertions whose semantics are identical to AssertJ are delegated. assertEquals /
assertNotEquals and the collection/map/array variants keep TestNG's own comparison semantics on
purpose — see the NOTE comment added to Assert#areEqualImpl. The only user-visible change is the
AssertJ-formatted failure messages on the delegated assertions.

Two commits:

1. Delegate behaviour-identical assertions to AssertJ

  • assertNull / assertNotNull / assertSame / assertNotSame and assertThrows → AssertJ.
  • Removed the now-unused failSame / failNotSame.
  • Marked org.testng.Assert @Deprecated (javadoc recommends AssertJ; not forRemoval — this
    artifact is the home of Assert); @SuppressWarnings("deprecation") on Assertion.
  • pom.xml: assertj-core moved from test to compile scope.
  • Added docs/MIGRATING_ASSERTIONS.md, linked from the README.

2. Delegate assertTrue / assertFalse / fail to AssertJ

  • assertTrue / assertFalseassertThat(condition).as(message).isTrue() / .isFalse().
  • fail(message) / fail(message, cause)org.assertj.core.api.Assertions.fail(...).
  • These flow through the OO Assertion / SoftAssert wrappers automatically (they delegate to
    org.testng.Assert), so soft assertions use AssertJ for these checks too — no change needed in
    SoftAssert / Assertion.
  • Updated four message-format-sensitive tests (behaviour assertions unchanged): the assertTrue /
    assertFalse / test2_fails message regexps now match AssertJ output, and
    SoftAssertTest#testAssertAllCount no longer counts report lines (AssertJ messages are
    multi-line) — it asserts the failed message is reported exactly once.

Notes

  • expectThrows is unchanged: it returns the caught exception, which AssertJ's fluent API does
    not expose.
  • fail is the shared failure sink for the (deliberately non-delegated) assertEquals / collection
    assertions. Delegating it routes those through AssertJ's AssertionFailedError (a subclass of
    AssertionError); the message text is still TestNG's (built by format(...)), only the
    exception subtype changes. The full suite confirms no assertEquals/collection test regressed.
  • The Lists/Maps → JDK refactor from the source PR is not included here (already done in this
    repo via c4d2bd2).

Testing

  • ./mvnw clean testBUILD SUCCESS, Tests run: 181, Failures: 0, Errors: 0, Skipped: 1.
  • ./mvnw dependency:tree confirms org.assertj:assertj-core:3.27.7:compile (testng/guava stay test).

🤖 Generated with Claude Code

juherr and others added 2 commits June 28, 2026 17:22
Port the AssertJ-backed assertion implementation from
testng-team/testng#3278 into this standalone artifact, so it can be
dropped from the parent PR.

- Assert: delegate assertNull/assertNotNull/assertSame/assertNotSame and
  assertThrows to AssertJ (behaviour-identical, only the failure message
  differs); remove the now-unused failSame/failNotSame helpers. Equality
  comparison intentionally keeps TestNG semantics (documented NOTE).
- Mark org.testng.Assert @deprecated (recommend AssertJ); add
  @SuppressWarnings("deprecation") on Assertion.
- pom: move assertj-core to compile scope (the main code depends on it).
- docs: add MIGRATING_ASSERTIONS.md and link it from the README.

expectThrows is kept unchanged (it returns the caught exception, which
AssertJ's fluent API does not expose).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Follow-up to the AssertJ delegation: extend it to the remaining
behaviour-identical entry points.

- assertTrue/assertFalse -> assertThat(condition).as(message).isTrue()/isFalse()
- fail(message) and fail(message, cause) -> org.assertj.core.api.Assertions.fail(...)

These flow through the OO Assertion/SoftAssert wrappers automatically
(they delegate to org.testng.Assert), so soft assertions now use AssertJ
for these checks too. Only the failure-message text changes; the
pass/fail behaviour is unchanged.

Updated the message-format-sensitive tests accordingly:
- assertTrue/assertFalse message regexps now match AssertJ's output.
- SoftAssertTest#testAssertAllCount no longer counts report lines (AssertJ
  messages are multi-line); it asserts the failed message is reported once.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@juherr juherr changed the title Delegate behaviour-identical assertions to AssertJ Back org.testng.Assert with AssertJ for behaviour-identical assertions Jun 28, 2026
@juherr juherr requested a review from krmahadevan June 28, 2026 15:40
Comment thread src/main/java/org/testng/Assert.java Outdated
* @deprecated TestNG no longer maintains its own assertion library. Use a dedicated assertion
* library such as <a href="https://assertj.github.io/doc/">AssertJ</a> instead. The OpenRewrite
* recipe {@code org.openrewrite.java.testing.testng.TestNgToAssertj} migrates the whole class
* automatically; see {@code docs/MIGRATING_ASSERTIONS.md} for details.

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.

Will docs/MIGRATING_ASSERTIONS.md be part of the codebase? We should probably instead replace it with the web url of the repository so that people can come here and view this file to learn about the migration plan

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, the URL will be better

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.

Can we fix this, so that we can get this merged?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/test/java/org/testng/AssertTest.java
Comment thread src/test/java/test/assertion/SoftAssertTest.java

@krmahadevan krmahadevan 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.

@juherr - Please check some of the observations that i have logged. I am not sure if they are discrepancies or not. Just wanted to still call them out.

Address review feedback (Krishnan): point the @deprecated javadoc to the
hosted migration guide URL instead of the in-repo file path, so it is
clickable from generated javadoc.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@krmahadevan krmahadevan merged commit 9e0ef6a into main Jun 30, 2026
1 check passed
@krmahadevan krmahadevan deleted the port-assertj-delegation branch June 30, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants