newbie
newbie

Reputation: 101

Unit testing error: java.util.NoSuchElementException: No value present at java.util.Optional.get

I'm trying to implement unit testing of my Object service below:

@Service
public class ObjectServiceImpl implements ObjectService{

  private final ObjectDao objectDao;

  @Autowired
  public ObjectServiceImpl(ObjectDao objectDao){
    this.objectDao = objectDao;
  }

  @Override
    public Object getById(Long ObjectId) throws RecordNotFoundException {
        Optional<Object> Object = ObjectDao.findById(ObjectId);
        if (!Object.isPresent()) {
            throw new RecordNotFoundException(String.format(
                    MessageConstants.ObjectID_NOT_FOUND, ObjectId));
        }
        return Object.get(); 
    }

However I get an error: No value present at java.util.Optional.get when I tried to implement my JUnit below:

@RunWith(MockitoJUnitRunner.Silent.class)
public class ObjectServiceTest {

    @InjectMocks
    private ObjectServiceImpl objectServiceImpl;

    @Mock
    private ObjectDao objectDao;

    private ObjectService objectService;

    @Test
    public void getById() throws RecordNotFoundException{
        Object object = new Object();
        Long objectId = 1L;
        Optional<Object> objectList =
            objectDao.findById(objectId);
        given(objectDao.findById(
                    objectList.get().getId())).willReturn(objectList);
        objectServiceImpl.getById(Mockito.anyLong());
        Mockito.verify(objectDao).findById(objectId);
        assertNotNull(objectServiceImpl.getById(objectId));
    }
}

Is there anything that I missed or that I need to update?

Upvotes: 4

Views: 13238

Answers (2)

gokareless
gokareless

Reputation: 1253

The first thing you should do here is to create a stub for objectList.

Afterwards you will describe objectDao behavior, and perform verification. My variant for the solution would be:

       @Test
       public void getById() throws RecordNotFoundException{
         // if it's List in your case
         Optional<Object> objectList = Optional.of(Arrays.asList(new YourType()));              
         given(objectDao.findById(Mockito.anyLong())).willReturn(objectList); 
         Object returnedObject = objectServiceImpl.getById(1L);
         Mockito.verify(objectDao).findById(Mockito.anyLong());
         assertNotNull(returnedObject);
      }

Note 1: There is redundant(never used) object at the beginning of your test

Note 2: Notice usage of Mockito.anyLong(), which I think more appropriate in your case(for testing service.getById();)

Upvotes: 3

pvpkiran
pvpkiran

Reputation: 27068

These two statements are not correct.

 Optional<Object> objectList =
        objectDao.findById(objectId);
 given(objectDao.findById(
        objectList.get().getId())).willReturn(objectList); 

First you are trying to do findById on a mock(objectDao) and then you are mocking what needs to be returned when this function is called.

One way to fix this is

 @Test
   public void getById() throws RecordNotFoundException{
     Object object = new Object();
     Long objectId = 1L;
     Optional<Object> objectList = // create a object of Type Object manually and assign it 
     given(objectDao.findById(
        objectId)).willReturn(objectList); 
     Object returnedobject = objectServiceImpl.getById(objectId);
     Mockito.verify(objectDao).findById(objectId);
     assertNotNull(returnedobject);
  }

Upvotes: 2

Related Questions