Java – Mock chain calls involve Streams in Java

Mock chain calls involve Streams in Java… here is a solution to the problem.

Mock chain calls involve Streams in Java

How to use stream simulation chain calls. Also note that it calls each.getName() as an intermediate operation.
I can’t create a SomeCountobject, so I have to simulate it as well.

Set<String> obj = new HashSet<String>();
List<SomeCount> someGroups = Some_Mocked_Implementation();
obj = -> each.getName()).filter(each -> 
                          userNames.indexOf(each) == -1)


You don’t know.

This is just the classic “input/output” test. You just want to create an input list with specific objects so that you can predict what the action should produce.

In other words, your test should essentially look like this:

assertThat(someMethodDoingTheStreamOperation(yourInputList), is(expectedResult));

Simulating containers such as List or Map is (almost always) wrong.

If you still insist on doing so, you may be using Mockito and its deep stubs support.

But again: this means that you start putting the implementation details of the solution into your test code. This means that your test code is nothing more than a “copy” of production code. When you change production code, your unit tests may break. Therefore, even simple refactorings can become a problem.

You always prefer tests that don’t rely on simulations. When talking about lists, hey: populate the list with prepared input, not mock the list.

Related Problems and Solutions