### Question:
ObjectiveService { @Transactional public KeyResult createKeyResult(Long objectiveId, KeyResult keyResult, User user) throws KeyResultOverflowException { Objective referencedObjective = findById(objectiveId); throwIfKeyResultLimitIsHit(referencedObjective); throwIfCycleForObjectiveIsClosed(referencedObjective); for (KeyResult otherKeyResult : referencedObjective.getKeyResults()) { otherKeyResult.setSequence(otherKeyResult.getSequence() + 1); keyResultRepository.save(otherKeyResult); } keyResult.setParentObjective(referencedObjective); keyResult = keyResultRepository.save(keyResult); logger.info( "Created Key Result " + keyResult.getName() + " in Objective " + referencedObjective.getName() + "(id:" + objectiveId + ")."); activityService.createActivity(user, keyResult, Action.CREATED); return keyResult; } @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 createKeyResult_expectsParentIdIsSet() throws KeyResultOverflowException { Long expected = 18L; objective.setId(expected); objectiveService.createKeyResult(expected, keyResult, user); Assert.assertEquals(expected, keyResult.getParentObjective().getId()); veryfyCreateKeyResult(); }
@Test public void createKeyResult_expectedOtherObjectivesSequenceAdvanced() throws KeyResultOverflowException { Long expected = 18L; objective.setId(expected); KeyResult otherKeyResult1 = new KeyResult(); otherKeyResult1.setSequence(5); KeyResult otherKeyResult2 = new KeyResult(); otherKeyResult2.setSequence(6); KeyResult otherKeyResult3 = new KeyResult(); otherKeyResult3.setSequence(10); Collection<KeyResult> otherKeyResults = Arrays.asList(otherKeyResult1, otherKeyResult2, otherKeyResult3); Objective parentObjective = new Objective(); parentObjective.setKeyResults(otherKeyResults); when(objectiveRepository.findByIdOrThrow(any(Long.class))).thenReturn(parentObjective); objectiveService.createKeyResult(expected, keyResult, user); Assert.assertEquals(6, otherKeyResult1.getSequence()); Assert.assertEquals(7, otherKeyResult2.getSequence()); Assert.assertEquals(11, otherKeyResult3.getSequence()); }
@Test(expected = ForbiddenException.class) public void createKeyResult_cycleOfObjectiveIsClosed_expectedForbiddenThrow() throws KeyResultOverflowException { Cycle closedCycle = new Cycle(); closedCycle.setCycleState(CycleState.CLOSED); when(entityCrawlerService.getCycleOfObjective(any())).thenReturn(closedCycle); objectiveService.createKeyResult(10L, new KeyResult(), user); }
@Test(expected = KeyResultOverflowException.class) public void createKeyResult_expectsKeyResultOverflowExceptionBecauseOfMAximumNumberOfKeyResultsReached() throws KeyResultOverflowException { int maxKeyResultsPerObjective = 7; for (int i = 0; i < maxKeyResultsPerObjective; i++) { objective.getKeyResults().add(new KeyResult()); } KeyResult keyResult = new KeyResult(); objectiveService.createKeyResult(anyLong(), keyResult, user); veryfyCreateKeyResult(); }