title: Test lambda expressions called by dependencies
body: <p>I am trying to test some code inside lambda expression which is a call back from another class.</p>
  
  <pre><code>class EmailSender {
      private EmailBuilder emailBuilder;
  
      public void send() {
          String testEmail = emailBuilder.buildEmail("Test Email", bodyContentAppender());
          //send testEmail
      }
  
      private Consumer&lt;Email&gt; bodyContentAppender() {
          //how to test this through JUnit?
          return email -&gt; email.appendBody("Body Content");
      }
  }
  
  interface EmailBuilder {
  
      String buildEmail(String templateName, Consumer&lt;Email&gt; contentAppender);
  }
  </code></pre>
  
  <p>The lambda expression in the method <code>getBodyContent</code> is called from <code>EmailBuilder</code> which is a mocked dependency in the JUnit test for <code>EmailSender</code>. Since I am mocking the behavior of <code>EmailBuilder</code>, the code inside <code>getBodyContent</code>is not called from  tests. How to test such piece?</p>
  
  <p><strong>EDIT:</strong>
  Capturing the lambda expression through Argument Captors is not a solution in this case as the behavior of <code>EmailBuilder</code> is mocked and the actual methods are not called. Secondly, <code>email.appendBody</code> does some transformations on an object which is passed by an external API and not straightforward to create.</p>