I want to assert the object which is returned from the method that I am testing in Junit using Assertions.AssertEquals but its giving test case failure even though two objects are same.
Expected :Customer{Id=1, Name='ABC', Price=350, Type='ABC'} Actual :Customer{Id=1, Name='ABC', Price=350, Type='ABC'}
This is the unit test case
@Test
public void getCustomerById_Test(){
Dishes test1 = repo.getById(1);
assertEquals(ExpectedObject,ActualObject);
}
CodePudding user response:
JUnit method assertEquals
simply uses equals
method of your class to compare the objects - you have to implement the method property for the assertion to work correctly. You can read more about implementing equals
in multiple online source, for example here.
If you don't want to implement equals
in the verified class, you can also use assertEquals
to compare object fields you're interested in, like so:
assertEquals(7, actual.getSomeValue());
assertEquals("abcd", actual.getOtherValue());
If you added the AssertJ library, you could use it's usingRecursiveComparison
method - for details refer to the documentation.