title: Use Mockito to write stub for Java 8 lambda expressions
body: <p>I have a method where I am using Java 8 lambda expression. While writing unit test for this method I need to write stub for that expression. How we can write that?</p>
  
  <pre><code>public List&lt;User&gt; methodA(String userId) {
      List&lt;User&gt; users = new ArrayList&lt;&gt;();
      userRepository.findAll().forEach(users::add);
      List&lt;User&gt; usersFilterByUserId = users.stream().filter(u -&gt; u.getUserid().equalsIgnoreCase(userId)).collect(Collectors.toList());
  
      some other stuff ....
  }
  </code></pre>
  
  <p>I tried solution given <a href="https://stackoverflow.com/questions/30081357/use-mockito-2-0-7-to-mock-lambda-expressions">here</a> as -</p>
  
  <pre><code>@Test
  public void testMethodA() {
      ArrayList&lt;User&gt; mockUsers = mock(ArrayList.class);
      PowerMockito.whenNew(ArrayList.class).withNoArguments().thenReturn(mockUsers);
      User user = mock(User.class);
      Iterable&lt;User&gt; mockIt = mock(Iterable.class);
      when(userRepository.findAll()).thenReturn(mockIt);
      doNothing().when(mockIt).forEach(any());
  
      // This gives compiler error which is obvious 
      // The method thenReturn(Stream&lt;User&gt;) in the type 
      // OngoingStubbing&lt;Stream&lt;User&gt;&gt; is not applicable for the arguments (ArrayList&lt;User&gt;)
      ArrayList&lt;User&gt; mockUsersFilterByUserId = mock(ArrayList.class);
      when(mockUsers.stream()).thenReturn(mockUsersFilterByUserId);
      ...
  }
  </code></pre>