Mockito how to test findById() returning an Optional

Viewed 14155

I'm stuck trying to test the findById() method from the CrudRepository. This method returns an Optional and I can't figure out how to return it, right now its giving me a NullPointerException.

My test code looks as following:

@RunWith(MockitoJUnitRunner.class)
public class DishServiceMockTest {

    private static final String DISH_NAME = "Kaas";
    private static final String DISH_TYPE = "Voorgerecht";
    private static final Long DISH_ID = 23L;

    //Mock the service dependencies(=DishServiceImpl is dependent on dishRepo)
    @Mock
    DishRepository dishRepository;

    //Mock the service which is to be tested (Can't be a interface)
    @InjectMocks
    DishServiceImpl dishService;

@Test
public void findById(){
    //Arange
    Dish dish = createDish(DISH_ID, DISH_NAME, DISH_TYPE);

    Mockito.when(dishRepository.findById(DISH_ID)).thenReturn(Optional.of(dish));
    assertThat(dishService.findById(DISH_ID)).isEqualTo(dish);

}

Running the test gives me 2 errors, one of which is a NullPointerException and the second one:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at avans.ivh11.proftaak.mocks.DishServiceMockTest.findById(DishServiceMockTest.java:85)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

NPE

java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Optional.<init>(Optional.java:96)
at java.util.Optional.of(Optional.java:108)
at avans.ivh11.proftaak.mocks.DishServiceMockTest.findById(DishServiceMockTest.java:85)

ComparisonFailure

org.junit.ComparisonFailure: 
Expected :avans.ivh11.proftaak.Domain.Dish@17
Actual   :Optional[avans.ivh11.proftaak.Domain.Dish@17]
2 Answers

This stacktrace

java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Optional.<init>(Optional.java:96)
at java.util.Optional.of(Optional.java:108)
at avans.ivh11.proftaak.mocks.DishServiceMockTest.findById(DishServiceMockTest.java:85)

says that dishfrom .thenReturn(Optional.of(dish)); is null. Double check your createDish method.

Second exception is an result of the first one in

 Mockito.when(dishRepository.findById(DISH_ID)).thenReturn(Optional.of(dish));

when is successfully called, but to invoke thenReturn Optiona.of must be evaluated - that one fails, so there is no thenReturn call ever made.

And acttually this is not "runtime" exception but post test exception.

Use doReturn(Optional.of(yourMockObj)).when(employeeRepository).findById(idToFind);

Sample Code

@Test
@Order(2)
@DisplayName("Test getEmployeeByEmployeeId")
public void test_getEmployeeByEmployeeId() throws EmployeeNotFoundException
{
    // Setup the mock repo
    Long employeeId = 1L;
    Employee e1ForMock = new Employee(employeeId, "Aravinth P", 29, "aravinth.p@email.com");
    doReturn(Optional.of(e1ForMock)).when(employeeRepository).findById(employeeId);
    // Make the service call
    Employee e1ByService = employeeServiceImpl.getEmployeeByEmployeeId(employeeId);
    // Assert the response
    assertNotNull(e1ByService,"Employee with employeeId : "+employeeId+" not found");
    assertEquals(employeeId,e1ByService.getEmployeeId());
    assertEquals(e1ForMock.getName(), e1ByService.getName());
    assertEquals(e1ForMock.getAge(), e1ByService.getAge());
    assertEquals(e1ForMock.getEmail(), e1ByService.getEmail());
}
Related