0

I know that this question has been asked before. However, none of the solutions are working for me. I have a user-defined class that is a composite of 3 classes. It looks like this:

Class compositeClass
{
UserDefinedClass1 useClass1;
UserDefinedClass2 useClass2;
UserDefinedClass3 useClass3;
}

I have a test method I need to compare Lists of these composite classes. I have tried several variations of the Assert equals like these: Hamcrest:

Assert.assertThat(mockCompositeList().getStandardLoadComposites(), 
                    equalTo(closeRequest.getStandardLoadComposites()));

Assert.assertArrayEquals(mockCompositeList().getStandardLoadComposites().toArray(), 
                closeRequest.getStandardLoadComposites().toArray());

Both instances return this AssertionError:

Expected < StanardLoadComposite @ 6895a785 > but was < StandardLoadComposite @ 184f6be2 >

It seems it is comparing the reference and not the values in the objects. Do I have to override the equals operator to get it to compare the values in the objects of the user defined objects?

Gloria Santin
  • 2,066
  • 3
  • 51
  • 124
  • Are the Hamcrest methods (from https://stackoverflow.com/questions/33840531/how-do-hamcrests-hasitems-contains-and-containsinanyorder-differ ) some of those that you have tried before ? – racraman Feb 18 '19 at 01:50
  • If you did try (for example) containsInAnyOrder, what wasn't working (ie. how was their result different from what you were expecting). Also, can you confirm that you have overridden equals() and hashcode() in your classes? – racraman Feb 18 '19 at 02:00
  • And sorry - I just reread : `Do I have to override the equals operator to get it to compare the values in the objects of the user defined objects?` Yes, you do have to override equals() - and whenever you override equals(), you should also override hashcode() as well (see https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html ) – racraman Feb 18 '19 at 02:11

2 Answers2

0

Consider comparing objects inside the list,instead comparing the list itself.I am assuming you have created one mock list, to assert the expected result and for the actual result(List) your method to be tested will create new list using new. Anyway this equals method will compare the id of three objects and return true if overridden.But consider overriding your equals and hash method just to make your assertion works fine.

Two lists need not be a same one.But the mock objects you have created and passed to your class should not changed after some point.If this is what you want to make sure in your test, then comparing the objects inside list will do that for u.

indhu
  • 56
  • 5
0

Thanks for the response. What I did was create a helper method that compared the values in the list. To override the equals operator for the composite class would be alot of work when there are only 3 values in the class that are set. So, I compared the values in each object in the list and returned true/false depending on the comparison. Then used the method in the assert equals and compared boolean.

Gloria Santin
  • 2,066
  • 3
  • 51
  • 124