title: Unit test code with Java 8 Lambdas
body: <p>I have been using Java 8 for some months, and I have started to use Lambda expressions, which are very convenient for some cases. However, I often come across some problems to unit test the code that uses a Lambda.</p>
  
  <p>Take as an example the following pseudo-code:</p>
  
  <pre><code>private Bar bar;
  
  public void method(int foo){
      bar.useLambda(baz -&gt; baz.setFoo(foo));
  }
  </code></pre>
  
  <p>One approach would be to just verify the call on bar</p>
  
  <pre><code>verify(bar).useLambda(Matchers.&lt;Consumer&lt;Baz&gt;&gt;.any());
  </code></pre>
  
  <p>But, by doing that, I don't test Lambda's code.</p>
  
  <p>Also note that I am not able to replace the Lambda with a method and use method reference:</p>
  
  <pre><code>bar.useLambda(This::setFooOnBaz);
  </code></pre>
  
  <p>Because I will not have the foo on that method. <em>Or at least that is what I think.</em></p>
  
  <p><strong>Have you had this problem before? How can I test or refactor my code to test it properly?</strong></p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>Since what I am coding is an unit test, I don't want to instantiate bar, and I will be using a mock instead. So I will not be able to just verify the <code>baz.setFoo</code> call.</p>