1

I have a method where I am using Java 8 lambda expression. While writing unit test for this method I need to write stub for that expression. How we can write that?

public List<User> methodA(String userId) {
    List<User> users = new ArrayList<>();
    userRepository.findAll().forEach(users::add);
    List<User> usersFilterByUserId = users.stream().filter(u -> u.getUserid().equalsIgnoreCase(userId)).collect(Collectors.toList());

    some other stuff ....
}

I tried solution given here as -

@Test
public void testMethodA() {
    ArrayList<User> mockUsers = mock(ArrayList.class);
    PowerMockito.whenNew(ArrayList.class).withNoArguments().thenReturn(mockUsers);
    User user = mock(User.class);
    Iterable<User> mockIt = mock(Iterable.class);
    when(userRepository.findAll()).thenReturn(mockIt);
    doNothing().when(mockIt).forEach(any());

    // This gives compiler error which is obvious 
    // The method thenReturn(Stream<User>) in the type 
    // OngoingStubbing<Stream<User>> is not applicable for the arguments (ArrayList<User>)
    ArrayList<User> mockUsersFilterByUserId = mock(ArrayList.class);
    when(mockUsers.stream()).thenReturn(mockUsersFilterByUserId);
    ...
}
Saurabh
  • 2,384
  • 6
  • 37
  • 52
  • 1
    It's quite unclear what you're trying to do - you're trying to mock all of the local variables, so it's possible you don't fully understand the goal of mocking. But `stream()` returns a `Stream`, not an `ArrayList`, so that's your proximate problem. – Oliver Charlesworth Dec 19 '17 at 18:26

1 Answers1

1

Actually, you mock everything in your unit test.
The test becomes complex and loose its value.

In methodA, the single thing that you should mock is the dependency :

userRepository.findAll().

By mocking the invocation to this method, you will by side effect use mocked data in the lambda body as it uses the result of findAll().

List<User> usersByMock = new ArrayList<>();
usersByMock.add(...);
usersByMock.add(...);
usersByMock.add(...);
...
when(userRepository.findAll()).thenReturn(usersByMock);
davidxxx
  • 125,838
  • 23
  • 214
  • 215