Keep in mind that the identity of method references or lambdas is not guaranteed to be the same even if the calls look identical. Check this out
I don't know much about Mockito, but I believe it checks if there exists a value that a when
registered, this might invlove a call to equals somewhere which is not guranteed to work here. For example:
Function<String, String> fn1 = String::toUpperCase;
Function<String, String> fn2 = String::toUpperCase;
System.out.println(fn1.equals(fn2)); // false on my machine
You can simply store the reference in a variable and use it later on
Function<String, String> toUpperCase = String::toUpperCase;
when(stringStream.map(toUpperCase)).thenReturn(upperCaseStream);
System.out.println(stringStream.map(toUpperCase));
Btw, I really don't understand why would ever need to mock Stream<String>
where you can simply do Stream.of("foo","bar")