Test lambda expressions called by dependencies
Most mocking frameworks allow you to check arguments that are used when invoking methods on mocked object. Respectively, you can capture them.
So:
- acquire the parameter passed
- simply invoke the "code" that it represents, and check if that makes the expected updates to an
Email
object you provided.
What you are trying to do here is essentially to verify that a factory method did in fact really return the correct object. There is this related question, where the consensus is to not test the result of a factory method beyond verifying that it does indeed return an object of the correct type. The behavior of that object should be tested in the UnitTests for that type.
In an answer to this related question on unit testing lambdas Stuart Marks argues that
If the code in the lambda is complex enough that it warrants testing, maybe that code ought to be refactored out of the lambda, so that it can be tested using the usual techniques.
Now, the real question is: If this was not a lambda, but a concrete class MyBodyContentAppender
that implements the functional interface Consumer<Email>
, how would you unit test that? What kinds of test would you write for this class?
You would probably write tests to verify that, given an Email
, invoking accept()
does indeed invoke appendBody()
with the appropriate parameters, perhaps that invoking it with a null
argument throws a NullPointerException
etc. You would possibly not verify that email.appendBody()
works as expected, because that is covered by the tests for Email
. You may have to mock Email
for these tests if it is difficult to create.
Well, all of these tests can also be performed for the lambda. Your problem is that the factory and the type of the created object are both private, so from the perspective of your test, the only way to access that object is via the parameter passed to the (mocked) emailBuilder.buildEmail()
.
If you use Mockito for mocking the emailBuilder
, you could capture the arguments to this method via ArgumentCaptor
s (see 15. Capturing arguments for further assertions (Since 1.8.0)), I'm sure other mocking libraries provide similar functionality.