Roar Skullestad
Roar Skullestad

Reputation: 2437

Sorting mock objects in mockito

I am testing a class with mockito. It contains these methods:

The class does several things to the objects internally, and it is dependant on sorting the Sortable objects.

The problem is that I am mocking Sortable, which makes compareTo() always return 0. Is there a way to make a Sorting mock that has the original Sortable compareTo() implementation? Or are there other ways to solve this?

Upvotes: 1

Views: 2902

Answers (1)

Megaprog
Megaprog

Reputation: 466

Use statement like this before mocked object compareTo() method calling:

when(mockedObject.compareTo(any(Sortable.class))).thenCallRealMethod();

Upvotes: 9

Related Questions