How to Mock Lambda Expression in Java?
Lambdas are anonymous functions that are defined at runtime, making it difficult to mock one in Java. But it is feasible to mock lambda expressions in Java and test them effectively with the aid of several potent mocking frameworks, like Mockito or PowerMock. Using Mockito, one of the most well-liked Java mocking frameworks, we'll examine how to mock lambda expressions in this post.
A well-liked mocking framework called Mockito lets you make fake objects and use unit tests to check how your code behaves. The capability to mock functional interfaces, even interfaces with a single abstract method, such as Runnable, Callable, or Comparator, is one of Mockito's most potent capabilities. Lambda expressions can be mocked using Mockito because they are functional interfaces as well.
Here is an illustration of how to use Mockito to mock an expression using lambda in Java:
The above code will not produce any output because it only contains a mocked Runnable object with a lambda expression that simply prints a message when it is executed. The lambda expression is executed by calling the run() method of the mocked Runnable object.
However, the output of the lambda expression execution ("Lambda expression has been executed.") will not be printed to the console, because the lambda expression does not contain any code that outputs to the console.
We're using Mockito in this instance to imitate a Runnable interface. The lambda expression's behaviour is defined using the doAnswer method. The message is being printed to the console in this instance, and null is being returned. Afterward, we use the run function to actually run the lambda expression and the verify method to make sure it actually was.
Using Mockito, the following example shows how to mock a lambda expression with arguments:
The output of this code will not be visible as it only contains a test case and no code to print any output. However, when this test case is executed using a testing framework such as JUnit, the framework will report whether the test passed or failed based on the assertions made in the test case.
In this case, the test case is testing whether the mock Comparator object is correctly executing a lambda expression that compares two strings case-insensitively. The verify statement checks whether the compare method was called with the arguments "Hello" and "world", and the assertEquals statement checks whether the returned result is 0, which indicates that the two strings are equal when compared case-insensitively. If both assertions pass, the test will pass, otherwise it will fail.
In this illustration, Mockito is being used to fake a Comparator interface. To define the operation of the lambda expression with arguments, we use the when method. We are comparing two strings in this instance without accounting for case. The lambda expression is then run using the compare method with the two inputs "Hello" and "world," and its proper execution is confirmed using the verify method.
In conclusion, it can be difficult to effectively test lambda expressions in Java, but it is achievable with the aid of strong mocking frameworks like Mockito. You may control how your lambda expressions behave and ensure that they were run with the right parameters by utilising the doAnswer and when methods. You should be able to confidently mimic lambda expressions while evaluating your code efficiently after reading this article's examples.