简体   繁体   中英

How to assert returning object in Java Springbok Unit

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);
}

JUnit method assertEquals simply uses equals method of your class to compare the objects - you have to implement the method properly for the assertion to work correctly. You can read more about implementing equals in multiple online sources, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM