6

I am wondering how can I force an immediate evaluate on each map function in Java8 ? The situation I have now is that I need to do multiple levels transforming (from ObjectA -> ObjectB -> ObjectC -> ObjectD), and there could be a failure (throw exception) on every level of this transforming for some object. For example

 // stream -> map -> collect sequence
 lists.stream()
      .map(aToB)
      .collect(Collectors.toList())
      .stream()
      .map(bToC)
      .collect(Collectors.toList())
      .stream()
      .map(cToD)
      .collect(Collectors.toList())

 // Try api is from javaslangs
 Function<ObjectA, ObjectB> aToB = a -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

 Function<ObjectB, ObjectC> bToC = b -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

 Function<ObjectC, ObjectD> cToD = c -> Try.of(() -> .....)
                                           .onFailure(....)
                                           .get();

I wanna to test each transition in my unit test such as test if the test thrown an exception, and if A unsuccessfully transformed to C when there's an exception when transforming A to B something like that, but with lazy evaluation, this becomes impossible to test, and the only way I can think of is to do this sequence of steam() -> map(...) -> collect(...) calls to force an immediate evaluation. I am wondering is there a better way to write this.

4
  • 2
    Why? What is wrong with just having all the map chained?
    – Tunaki
    Feb 1, 2016 at 22:21
  • 1
    I don’t understand the problem. When you collect the final result, all functions have been applied to all stream elements, or, if any evaluation fails, the exception will get propagated to the caller, so you have tested all functions. In the case of an exception, the stack trace will tell you, which function has failed. So what kind of problem are you trying to solve with these intermediate .collect(Collectors.toList()).stream()? They don’t change anything besides performance here.
    – Holger
    Feb 2, 2016 at 10:41
  • Are different exceptions thrown in each case, or the same exception? Feb 2, 2016 at 18:27
  • 1
    If what you want is N separate evaluations, write N stream pipelines, and test the results immediately after each one. Trying to do it in one big expression is just a distraction. Feb 2, 2016 at 21:29

2 Answers 2

4

Some simple thoughts here:

  • Test the whole chain of streaming operations on single-element collections. Verify that exceptions are thrown where you expect.
  • Don't worry about testing the stream framework; just apply the functions directly, yourself, in tests, rather than going through the streams.

Generally speaking, don't try to deal with the lazy evaluation in your tests: test the functions, and trust the stream framework to do the right thing in real code.

4
  • You mean write test for a->b, b->c, and c->d individually and independently instead of a->b->c->d as a whole?
    – peter
    Feb 1, 2016 at 22:30
  • 1
    @peter sure, or both, but mainly test on individual elements instead of streams of elements. Feb 1, 2016 at 22:31
  • just to see if I get this right....so in my test I would do this immediate evaluation manually...but in dev i should just trust it to be lists.stream().map(aToB).map(bToC).map(cToD).collect(...) like that ?
    – peter
    Feb 1, 2016 at 22:34
  • 1
    @peter, yes, that's the idea. Feb 1, 2016 at 22:34
1

You can write a static helper method that collects into a collection and then returns that collection's stream and wrap individual steps of the stream pipeline into that helper.

You should be aware that this alters the semantics of the stream.

For example an infinite stream source might fail if handled in this matter even though the complete stream pipeline might succeed if it is terminated with a short-circuiting operation. Similarly parallelism and concurrency properties might get altered. So you would end up testing something different from the stream pipeline itself.

Instead it's probably a better idea to just test the full stream pipeline to deliver the expected outputs for possible input scenarios and possibly test the mapping Function separately on various kinds of inputs.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.