1

I am testing a java method which uses lambda expression to match a value.

I want to test the condition for both true and false.

employee.getAppraisals().stream().anyMatch(appraisal::isPromoted)

And i tried to use the code below to create the mockito unit test but it never works. The link i followed is : Use Mockito 2.0.7 to mock lambda expressions

Mockito.when(employee.getAppraisals()
      .stream()
      ..anyMatch(p->p.equals(Mockito.any()))).thenReturn(true)

But i dont know how to mock Promotion object to return its bean proeprty value of promotion.isPromoted as true using mockito.

DevMind
  • 13
  • 4

1 Answers1

0

You do not want to mock the whole stream() chain.

You want to mock two things here, getAppraisals() which will return a collection of mock(Appraisal.class).

These Appraisal mocks will then answer to the method call for isPromoted().

Guillaume F.
  • 5,905
  • 2
  • 31
  • 59