Mandroid
Mandroid

Reputation: 7468

Mocking subsequent calls to a method with one of the two argument same

In a method I need to unit test, following is the situation:

Predicate<MyClass> predicate1 = (MyClass myClass) -> myClass.getX().equals(SOME_VALUE);

List<MyClass> targets = this.filterMyClass(listOfMyClass, predicate1);

if (CollectionUtils.isEmpty(targets)) {
    Predicate<MyClass> predicate2 = (MyClass myClass) -> myClass.getX().equals(SOME_OTHER_VALUE);
    targets = this.filterMyClass(listOfMyClass, predicate2);
}

As can be seen, filterMyClass() is called two times with first argument the same (a list) while second argument is different (a predicate).

How can calls to filterMyClass be mocked?

Upvotes: 0

Views: 41

Answers (1)

Raghvendra Garg
Raghvendra Garg

Reputation: 515

Mockito.when(myClass.getX).thenReturn(SOME_VALUE, SOME_OTHER_VALUE)

this will return SOME_VALUE for first invocation and SOME_OTHER_VALUE for second invocation.

Upvotes: 2

Related Questions