### Question:
ObjectiveService { @Transactional public void deleteObjectiveById(Long objectiveId, User user) { Objective referencedObjective = objectiveRepository.findByIdOrThrow(objectiveId); throwIfCycleForObjectiveIsClosed(referencedObjective); for (Objective subObjective : referencedObjective.getSubObjectives()) { subObjective.setParentObjective(null); objectiveRepository.save(subObjective); logger.info( "Removed parent objective from " + referencedObjective.getName() + "(id:" + referencedObjective.getId() + ")."); } if (referencedObjective.getParentOkrUnit() != null) { for (Objective otherObjective : referencedObjective.getParentOkrUnit().getObjectives()) { if (otherObjective.getSequence() > referencedObjective.getSequence()) { otherObjective.setSequence(otherObjective.getSequence() - 1); objectiveRepository.save(otherObjective); } } } objectiveRepository.deleteById(objectiveId); activityService.createActivity(user, referencedObjective, Action.DELETED); } @Autowired  ObjectiveService(
      ParentService parentService,
      ObjectiveRepository objectiveRepository,
      KeyResultRepository keyResultRepository,
      ActivityService activityService,
      EntityCrawlerService entityCrawlerService,
      ConfigurationService configurationService,
      @Qualifier("okrUnitServiceUsers") OkrUnitServiceUsers<OkrChildUnit> unitService); Objective findById(Long objectiveId); Collection<KeyResult> findKeyResultsOfObjective(long objectiveId); @Transactional Objective updateObjective(Objective updatedObjective, User user); @Transactional void deleteObjectiveById(Long objectiveId, User user); @Transactional KeyResult createKeyResult(Long objectiveId, KeyResult keyResult, User user); @Transactional void updateSequence(Long unitId, Collection<Long> sequenceList, User user);  }### Answer:
@Test public void test_deleteObjective_ExpectedObjectiveIsDeleted() { when(objectiveRepository.findByIdOrThrow(objectiveId)).thenReturn(objective); objectiveService.deleteObjectiveById(objectiveId, user); verify(objectiveRepository).deleteById(objectiveId); }
@Test public void deleteObjective_expectedOtherObjectiveSequenceAdjusted() { OkrDepartment parentOkrDepartment = new OkrDepartment(); Objective objectiveBelowInSequence = new Objective(); objectiveBelowInSequence.setSequence(5); Objective objectiveToDelete = new Objective(); objectiveToDelete.setParentOkrUnit(parentOkrDepartment); objectiveToDelete.setSequence(6); Objective objectiveAboveInSequence = new Objective(); objectiveAboveInSequence.setSequence(7); Collection<Objective> otherObjectives = Arrays.asList(objectiveAboveInSequence, objectiveBelowInSequence, objectiveToDelete); parentOkrDepartment.setObjectives(otherObjectives); when(objectiveRepository.findByIdOrThrow(objectiveId)).thenReturn(objectiveToDelete); objectiveService.deleteObjectiveById(objectiveId, user); Assert.assertEquals(5, objectiveBelowInSequence.getSequence()); Assert.assertEquals(6, objectiveAboveInSequence.getSequence()); }
@Test public void test_deleteObjective_ExpectSubObjectivesToSetParentObjectiveGotUpdated() { Objective parentObjective = new Objective(); Long parentObjectiveId = 4231L; parentObjective.setId(parentObjectiveId); this.objective.setSubObjectives(Collections.singletonList(parentObjective)); parentObjective.setParentObjective(this.objective); when(objectiveRepository.findByIdOrThrow(objectiveId)).thenReturn(objective); objectiveService.deleteObjectiveById(objectiveId, user); verify(objectiveRepository).save(parentObjective); }
@Test public void test_deleteObjective_ExpectedActivityGotCreated() { when(objectiveRepository.findByIdOrThrow(objectiveId)).thenReturn(objective); objectiveService.deleteObjectiveById(objectiveId, user); verify(activityService).createActivity(user, this.objective, Action.DELETED); }