4

I have the following code with a lambda function:

obj.method(param, () -> { 
      //code here
})

How can I cover by test the code in the lambda function?

4
  • 4
    It all depends on what obj.method() does with it. If it calls it immediately, then just call the method that calls obj.method(). If it saves it for later, then make sure the later operation is executed by your test. Commented Aug 29, 2015 at 8:57
  • 3
    Can you share a fuller snippet please? Commented Aug 29, 2015 at 8:57
  • 5
    I would tend to turn non-trivial lambda functions into methods and simply use a method reference. This way you keep the advantage that the lambda affords - succinct code to pass callbacks - but also keep the advantage that a method affords - visibility as separate units. Commented Aug 29, 2015 at 9:14
  • 2
    Wouldn't the advice to not test private methods apply here? Test the behavior that you expect from the public method which uses this lambda, and you'll save yourself headache if you refactor later. Commented Aug 29, 2015 at 11:11

1 Answer 1

3

You could use reflection, but this is likely to be error prone and counter productive.

I suggest you call the method which uses the lambda.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.