0

I have trouble with comparing two object.

var response = api.getRequest();
response.statusCode(200);

response.body(JsonSchemaValidator.matchesJsonSchema....);

var bodyResponse = response.extract().as(ExampleResponse.class);
var bodyExpected = ExampleResponse.builder()...build().

assertThat("Response body is not the same with expected body",bodyResponse, equalTo(bodyExpected));

In test I'm getting error that response body and expected are not the same :/

I couldn't upload image with compared object but belive me, they are the same (I checked in debugger) :)

Thanks for any tips :)

Marcin
  • 97
  • 7
  • 2
    Is the `equals` method overridden in `ExampleResponse `? By default in java, 2 references are equals only if they are exactly the same instance. – Matt Oct 13 '22 at 08:36
  • I think I don't understand what you mean. Can you explain more detalistic :) – Marcin Oct 13 '22 at 08:41
  • `new Object().equals(new Object())` is false, because the `equals` method is not overridden. If you haven't overridden this method, two instances cannot be tested for equality. – knittl Oct 13 '22 at 10:36
  • Thanks for explanation, but I think I found another solution using is(equalTo()) So now, code looks like that : assertThat("Response body is not the same with expected body", bodyResponse, is(equalTo(bodyExpected))); and it is working :) – Marcin Oct 13 '22 at 11:04
  • @Marcin that's odd, given that `is` doesn't do anything, it exists [solely to make tests read nicer](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#is(org.hamcrest.Matcher)). – knittl Oct 13 '22 at 11:24

1 Answers1

0

Java uses the equals Method to check for equality. You need to implement this!

See here

Josef
  • 1,467
  • 2
  • 24
  • 40