Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito: assertEquals on two lists, comparing object variables

I want to use assertEquals() on the two lists list1 and list2.

The problem is, that I don't want to compare the lists like assertEquals(list1, list2). The elements are objects that can return their ids with a function getId(), and I want to check if the lists are equal in terms of the ids (ordered!).

How can I do that? Is functional programming a good idea?

like image 487
Yanick Nedderhoff Avatar asked Oct 20 '25 14:10

Yanick Nedderhoff


1 Answers

If you create the expected list with the ids of the objects in order, example

List<MyObject> expected = new ArrayList<>();
expected.add(myObject1); //with id = 1
expected.add(myObject2); //with id = 2
expected.add(myObject3); //with id = 3

assertEquals(expected, actual);

, then if the actual list has objects in different order of ids, it would fail.

like image 82
Rahul Sharma Avatar answered Oct 22 '25 03:10

Rahul Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!