Spring Data JPA findById() method returning null instead of Empty Optional

12,472

Solution 1

In a comment you state that this is in a test with mocked dependencies. The mocking takes Spring Data JPA out of the picture completely as it now is just a proxy implemented by a mock from Mockito.

The default behavior for a mock is to return null.

By default, for all methods that return a value, a mock will return either null, a primitive/primitive wrapper value, or an empty collection, as appropriate. For example 0 for an int/Integer and false for a boolean/Boolean.

As you are running with a mock you will need to instruct it to return an Optional.empty() else you will get null.

NOTE: You might want to create an improvement request for Mockito to default return Optional.empty in the case of an Optional return type.

Solution 2

What is the implementation of your Repository Class? The following repository and test case works for me.

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonRepoTest {

    @Autowired
    private PersonRepository personRepository;

    @Test
    public void testFindById(){
        Optional<Person> byId = personRepository.findById(1);
        Assert.assertTrue(byId != null);
    }
}

public interface PersonRepository  extends CrudRepository<Person, Integer> {
}
Share:
12,472

Related videos on Youtube

Akhil Prajapati
Author by

Akhil Prajapati

Updated on September 15, 2022

Comments

  • Akhil Prajapati
    Akhil Prajapati over 1 year

    I have a method that is making use of Spring Data JPA's findById() method is supposed to return an Optional. However, in case the entity is not found by the specified id, it is returning null instead of an Empty Optional.

     public TicketEntity findTicket(String ticket) throws EntityNotFoundException {
    
        Optional<TicketEntity> op = ticketEntityRepository.findById(ticket);
    
        TicketEntity ticketEntity = op.orElseThrow(() -> new EntityNotFoundException("ticket with the id " + ticket + " not found in the system"));
    
        return ticketEntity;
    }
    

    While debugging, I found that the value of op is null. This is the piece of code that is failing. I am using Spring Data JPA 2.0.8.RELEASE. Please help

    • gagan singh
      gagan singh almost 6 years
      @YCF_L code is generated by spring data jpa
    • Akhil Prajapati
      Akhil Prajapati almost 6 years
      findById() is a method defined by Spring-data-jpa. I have not defined this method
    • Jens Schauder
      Jens Schauder almost 6 years
      Please provide a reproducable testcase and file a bug. This shouldn't happen.
    • M. Deinum
      M. Deinum almost 6 years
      And null is the default response for a mock. You will have to tell the mock that it needs to return an Optional.empty().
  • Fatmajk
    Fatmajk about 4 years
    How would you instruct it to do that? Given the example code line: Mockito.when(repository.findById(Mockito.any()).orElse(null)‌​).thenReturn(myValue‌​);