Search code examples
javajunitmockito

How to test two different objects have same values


I am writing test for my code with Mockito and JUnit. Everything was so good so far but know I am having StackOverFlow error. I think, I am getting that error because my objects are nested. For example A object has relation with B object and B object also has relation with A object.

I am testing if my objects have same values like this

assertEquals(objectA.toString(), objectB.toString());

I want to delete @ToString annotation from my both object to handle StackOverFlow error but I do not know how to test two different objects are equal or no. I saw some topics in here and they are telling use assertThat but I do not have that too.

how can I test that the values of two different objects are the same? Also, if you have any other solution, I would like to hear.


Solution

  • First of all, thanks all of you guys. I have too many objects and attributes. Therefore, your suggestions require a lot of changes on the source code. I didn't want to make that while I am just testing my services. I found similar problem in here. I fixed the problem with @ToString.Exclude annotation.