Search code examples
javaunit-testinglambdamockitopowermockito

Use Mockito to write stub for Java 8 lambda expressions


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);
    ...
}

Solution

  • 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);