September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's start by testing a lambda that is not wrapped in a method. For example, the following lambda is associated with a field (for being reused), and we want to test its logic:
public static final Function<String, String> firstAndLastChar = (String s) -> String.valueOf(s.charAt(0)) + String.valueOf(s.charAt(s.length() - 1));
Let's take into account that a lambda generates an instance of a functional interface; then, we can test the behavior of that instance as follows:
@Testpublic void testFirstAndLastChar() throws Exception { String text = "Lambda"; String result = firstAndLastChar.apply(text); assertEquals("La", result);}