178. Testing methods that use lambdas

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:

@Test
public void testFirstAndLastChar() throws Exception {

String text = "Lambda";
String result = firstAndLastChar.apply(text);
assertEquals("La", result);
}
Another solution consists of wrapping the lambda in a method call and writing unit tests for the method call.

Often, the lambdas are used inside methods. For most cases, testing the method that contains the lambda is acceptable, but there are cases when we want to test the lambda itself. A solution to this problem consists of three main steps:

  1. Extracting the lambda in a static method
  2. Replacing the lambda with a method reference
  3. Testing this static method

For example, let's consider the following method:

public List<String> rndStringFromStrings(List<String> strs) {

return strs.stream()
.map(str -> {
Random rnd = new Random();
int nr = rnd.nextInt(str.length());
String ch = String.valueOf(str.charAt(nr));

return ch;
})
.collect(Collectors.toList());
}

Our goal is to test the lambda from this method:

str -> {
Random rnd = new Random();
int nr = rnd.nextInt(str.length());
String ch = String.valueOf(str.charAt(nr));

return ch;
})

So, let's apply the preceding three steps:

  1. Let's extract this lambda in a static method:
public static String extractCharacter(String str) {

Random rnd = new Random();
int nr = rnd.nextInt(str.length());
String chAsStr = String.valueOf(str.charAt(nr));

return chAsStr;
}
  1. Let's replace the lambda with the corresponding method reference:
public List<String> rndStringFromStrings(List<String> strs) {

return strs.stream()
.map(StringOperations::extractCharacter)
.collect(Collectors.toList());
}
  1. Let's test the static method (which is the lambda):
@Test
public void testRndStringFromStrings() throws Exception {

String str1 = "Some";
String str2 = "random";
String str3 = "text";

String result1 = extractCharacter(str1);
String result2 = extractCharacter(str2);
String result3 = extractCharacter(str3);

assertEquals(result1.length(), 1);
assertEquals(result2.length(), 1);
assertEquals(result3.length(), 1);
assertThat(str1, containsString(result1));
assertThat(str2, containsString(result2));
assertThat(str3, containsString(result3));
}
It is advisable to avoid lambdas that have more than one line of code. Therefore, by following the preceding technique, the lambdas become easy to test.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset