title: How to use Mockito to mock java stream and filter operation
body: <p>I've been searching and banging my head on this for a while, and was wondering if anyone out here had any idea how to mock, or if it's even possible to mock this code.</p>
  <p>Here's the code. I need to mock 'programs' and have it return a designated 'Program' object:</p>
  <pre><code>private Program getProgramFromClient(Client client) {
      return programs.stream().filter(
              program -&gt; program.getProgramCode()
                  .equalsIgnoreCase(client.getProgramCode()))
          .collect(Collectors.toList()).get(0);
  }
  </code></pre>
  <p>I'm presently using the @Mock annotations on these objects as follows:</p>
  <pre><code>@Mock
  private Program program;
  
  @Mock
  private List&lt;Program&gt; programs;
  </code></pre>
  <p>Then in the test class I have the following...</p>
  <pre><code>@BeforeEach
  void setUp() {
  
     ...
  
     programs = new ArrayList&lt;&gt;();
  
  </code></pre>
  <p>and then in the test method I have this:</p>
  <pre><code>when(programs.stream().filter(any()).collect(any())).thenReturn(programs);
  when(programs.get(any())).thenReturn(program);
  </code></pre>
  <p>When I run the test, I get the following errors:</p>
  <pre><code>java.lang.NullPointerException
  </code></pre>
  <p>Which is referring to this line:</p>
  <pre><code>when(programs.stream().filter(any()).collect(any())).thenReturn(programs);
  </code></pre>
  <p>When I click the error, the cursor moves to the beginning of 'filter', so it looks like it's saying programs is empty, which I suppose is true, but I'm (trying) to mock it.
  Do I need to make it an un-mocked list?
  i.e. a real list with the program added?
  I think I tried this earlier and still ran into trouble, but If someone suggests it again, I can re-try.</p>
  <p>The following is also part of the error message, but my feeble and burnt brain is having trouble understanding exactly what this all means at the moment:</p>
  <pre><code>You cannot use argument matchers outside of verification or stubbing.
  Examples of correct usage of argument matchers:
      when(mock.get(anyInt())).thenReturn(null);
      doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
      verify(mock).someMethod(contains(&quot;foo&quot;))
  
  This message may appear after an NullPointerException if the last matcher is returning an object 
  like any() but the stubbed method signature expect a primitive argument, in this case,
  use primitive alternatives.
      when(mock.get(any())); // bad use, will raise NPE
      when(mock.get(anyInt())); // correct usage use
  
  Also, this error might show up because you use argument matchers with methods that cannot be mocked.
  Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
  Mocking methods declared on non-public parent classes is not supported.
  
  </code></pre>
  <p>As always, any and all help is tremendously appreciated!</p>