### Question:
KeyResultService { @Transactional public void deleteKeyResult(Long keyResultId, User user) { KeyResult referencedKeyResult = keyResultRepository.findByIdOrThrow(keyResultId); throwIfCycleOfKeyResultIsClosed(referencedKeyResult); if (referencedKeyResult.getParentObjective() != null) { Collection<KeyResult> keyResultList = referencedKeyResult.getParentObjective().getKeyResults(); for (KeyResult keyResult : keyResultList) { if (keyResult.getSequence() > referencedKeyResult.getSequence()) { keyResult.setSequence(keyResult.getSequence() - 1); keyResultRepository.save(keyResult); } } } keyResultRepository.deleteById(keyResultId); activityService.createActivity(user, referencedKeyResult, Action.DELETED); } @Autowired  KeyResultService(
      KeyResultRepository keyResultRepository,
      NoteRepository noteRepository,
      ActivityService activityService,
      EntityCrawlerService entityCrawlerService,
      ObjectiveService objectiveService); KeyResult findById(long keyResultId); Collection<Note> findNotesOfKeyResult(long keyResultId); @Transactional KeyResult updateKeyResult(KeyResult updatedKeyResult, User user); @Transactional void deleteKeyResult(Long keyResultId, User user); @Transactional Note createNote(long keyResultId, Note note, User user); @Transactional void updateSequence(Long objectiveId, Collection<Long> sequenceList, User user);  }### Answer:
@Test(expected = EntityNotFoundException.class) public void deleteKeyResult_expectsEntityNotFoundExceptionIsThrown() { when(keyResultRepository.findByIdOrThrow(any(Long.class))) .thenThrow(new EntityNotFoundException()); keyResultService.deleteKeyResult(keyResultId, user); }
@Test(expected = ForbiddenException.class) public void deleteKeyResult_cycleOfKeyResultIsClosed_expectedForbiddenThrow() { Cycle closedCycle = new Cycle(); closedCycle.setCycleState(CycleState.CLOSED); when(entityCrawlerService.getCycleOfKeyResult(any())).thenReturn(closedCycle); keyResultService.deleteKeyResult(10L, user); }
@Test public void deleteKeyResult_expectedOtherKeyResultsSequenceAdjusted() { Objective parentObjective = new Objective(); KeyResult keyResultBelowInSequence = new KeyResult(); KeyResult keyResultToDelete = new KeyResult(); KeyResult keyResultAboveInSequence = new KeyResult(); Collection<KeyResult> keyResultList = Arrays.asList(keyResultAboveInSequence, keyResultBelowInSequence, keyResultToDelete); parentObjective.setKeyResults(keyResultList); keyResultBelowInSequence.setSequence(5); keyResultToDelete.setSequence(6); keyResultAboveInSequence.setSequence(7); keyResultToDelete.setParentObjective(parentObjective); when(keyResultRepository.findByIdOrThrow(any(Long.class))).thenReturn(keyResultToDelete); keyResultService.deleteKeyResult(10L, user); Assert.assertEquals(5, keyResultBelowInSequence.getSequence()); Assert.assertEquals(6, keyResultAboveInSequence.getSequence()); }