My method looks like this
public EP updateEP(long id, EP eP) {
EE eE = eRepo.findById(id).orElseThrow(EntityNotFoundException::new);
//some code
}
and my test method looks like this
@Test
public void testUpdateEWhenEExists() {
long id = 1l;
EE eE = new EE();
eE.setPosId(1l);
eE.setPosName("pos");
EPE ePE = new EPE();
ePE.setEId(id);
when(eRepo.findById(id).orElseThrow(EntityNotFoundException::new)).thenReturn(eE);
//some code
}
And it always throw EntityNotFoundException
.I want to be returned to me eE instead of EntityNotFoundException
EDIT
@Test
public void testUpdateEPWhenEExists() {
long id = 1l;
EE eE = new E();
eE.setPositionId(1l);
eE.setPosName("pos");
EPE ePE = new EPE();
ePE.setEId(id);
when(eRepo.findById(id)).thenReturn(Optional.of(eE));
}
In this case error is
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
EPE cannot be returned by findById()
findById() should return Optional