I\'m working on a weird issue, I was doing integration testing, calling my controller to get an object from database that doesn\'t exist.
public Optional<
In Spring @Repository classes, the getOne(id) method doesn't always verify existence until the object is queried (by calling entity.getId() or something) so it's no-such-entity exception may be delayed. To validate right away, use findById(id) instead (which returns an Optional which will be empty if the entity with that id doesn't exist).
This is how it worked for me
public User findUserById(Long id) {
return userRepository.findById(id).orElse(null);
}