Assertj-core: Add `doesNotHaveSameHashCodeAs`

Created on 22 Apr 2021  路  8Comments  路  Source: assertj/assertj-core

Summary

To achieve the most of the fluent api with hasSameHashCodeAs the counterpart doesNotHaveSameHashCodeAs would be nice.

Example

        final Person spiderman = new Person("spiderman");
        final Person otherSpiderman = new Person("spiderman");
        final Person batman = new Person("batman");

        assertThat(spiderman)
            .hasSameHashCodeAs(otherSpiderman)
            .doesNotHaveSameHashCodeAs(batman);

instead not we have to write

        final Person spiderman = new Person("spiderman");
        final Person otherSpiderman = new Person("spiderman");
        final Person batman = new Person("batman");

        assertThat(spiderman.hashCode())
            .isEqualTo(otherSpiderman.hashCode())
            .isNotEqualTo(batman.hashCode());

The combination of isNotEqualTo and .hashCode() with hasSameHashCodeAs is not possible.

good first issue

Most helpful comment

@joel-costigliola @derTobsch Thanks. I will get started now :)

All 8 comments

fair enough is you rename it to doesNotHaveSameHashCodeAs ;-)

Hi, I'd like to implement this

Hi, I'd like to implement this

That is ok for me, otherwise I would implement it. But if you want, go for it :)

@mct10 up to you, let us know ;-)

@joel-costigliola @derTobsch Thanks. I will get started now :)

Sorry to bother you again, but I want to clarify my understanding of this issue. Please correct me if I misunderstand you.
I think your problem is, you cannot use assertThat(spiderman).hasSameHashCodeAs(otherSpiderman).isNotEqualTo(batman.hashCode()), so you want a doesNotHaveSameHashCodeAs implemented. However, there is already doesNotHaveSameHashCodeAs in AbstractAssert.java. And I also think you can use isNotEqualTo and .hashCode(), together with hasSameHashCodeAs.
Could you please correct my understanding? Is doesNotHaveSameHashCodeAs what you want to implement? Thanks a lot.

@mct10 good catch, I guess I don't know the AssertJ API that well :laughing:
Here's an example https://www.javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/api/AbstractAssert.html#doesNotHaveSameHashCodeAs(java.lang.Object).

@derTobsch I'm closing this issue since I believe this should address your issue, but I'll reopen it if you tell me it's not.

Hehe, sry. I am using 3.18.1 with the latest Spring Boot version 2.4.5 and therefore I could not see it in the IDE and I haven't checked newer versions. So therefore I am glad that it is there and will upgrade to the latest version. Thanks all! :)

Was this page helpful?
0 / 5 - 0 ratings