Search code examples
javaspring-bootspring-mvcjunitmockito

Mockito how to test findById() returning an Optional


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]

Solution

  • 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.