Custom assertions use primitive comparison, not assertions

Open
#133 0 comments 1 reaction 1 assignee View on GitHub

@scordio is already working on this.

Since Oct 10, 2023.

Assessment

This issue has not been assessed yet.

Description

https://assertj.github.io/doc/#assertj-core-custom-assertions-creation shows how to create custom assertion classes. One part that I find very confusing is this piece of code in the custom hasName assertion implementation:

    // check assertion logic
    if (!Objects.equals(actual.getName(), name)) {
      failWithMessage("Expected character's name to be <%s> but was <%s>", name, actual.getName());
    }

I would expect to reuse existing String assertions there, e.g. to write this instead:

    // check assertion logic
    Assertions.assertThat(actual.getName)
        .withFailMessage(() -> "Expected character's name to be <%s> but was <%s>", name, actual.getName())
        .isEqualTo(name);

Is there a good reason to not use other existing assertions there (like confusing the logic to shorten stack traces or similar internal things)? If yes, can we please document it? Otherwise, shouldn't we change the example code?

I typically do use existing assertions in my own assertions, and I have this nagging feeling of doing something wrong without knowing what exactly. :)

Dominant language
CSS
Stars
26
Forks
32
Avg merge
1d 19h
Merged PRs (30d)
4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from assertj/doc

All issues in assertj/doc

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.