Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom assertions use primitive comparison, not assertions #133

Open
Bananeweizen opened this issue Dec 30, 2022 · 0 comments
Open

Custom assertions use primitive comparison, not assertions #133

Bananeweizen opened this issue Dec 30, 2022 · 0 comments
Assignees

Comments

@Bananeweizen
Copy link
Contributor

Bananeweizen commented Dec 30, 2022

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. :)

@scordio scordio self-assigned this Oct 10, 2023
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

No branches or pull requests

2 participants