R
R
Ryabos2019-07-08 18:24:11
Java
Ryabos, 2019-07-08 18:24:11

Is there a way to compare objects using their getters?

The test needs to compare two objects by accessing their getters. But I do not want to write such checks:

Assertions.assertThat(this.getName()).isEqualTo(that.getName());
Assertions.assertThat(this.getAddress()).isEqualTo(that.getAddress());
Assertions.assertThat(this.getAge()).isEqualTo(that.getAge());
...

AssettJ has the ability to compare objects using their field values, but it's the getters that I need to compare.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2019-07-08
@EreminD

you need to override the equals and hashCode methods of the class
idea it can generate (Alt + Insert)
then you will just write this.equals(that)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question