### Question:
OkrBranchService { public OkrBranch update(long branchId, OkrBranch okrBranch, User user) { throwIdDeviationExceptionIfIdsAreDeviating(branchId, okrBranch); OkrBranch persistedEntity = okrBranchRepository.findByIdOrThrow(branchId); persistedEntity.setName(okrBranch.getName()); persistedEntity.setParentOkrUnit(okrBranch.getParentOkrUnit()); persistedEntity.setOkrChildUnits(okrBranch.getOkrChildUnits()); persistedEntity.setObjectives(okrBranch.getObjectives()); okrBranchRepository.save(persistedEntity); logger.info("Update OkrBranch " + okrBranch.getName() + " (id: " + okrBranch.getId() + ")"); activityService.createActivity(user, okrBranch, Action.EDITED); return persistedEntity; } @Autowired  OkrBranchService(
      OkrBranchRepository okrBranchRepository, ActivityService activityService); OkrBranch findById(Long id); OkrBranch create(OkrBranch okrBranch, User user); OkrBranch update(long branchId, OkrBranch okrBranch, User user); void delete(Long id, User user);  }### Answer:
@Test public void update_expectEntityNotFoundExceptionIfThereIsNoSuchEntity() { long id = 444L; when(okrBranchRepository.findByIdOrThrow(id)).thenThrow(EntityNotFoundException.class); okrBranch.setId(id); try { okrBranchService.update(id, okrBranch, mockedUser); Assert.fail(); } catch (Exception ex) { assertThat( "Should only throw EntityNotFoundException.", ex, instanceOf(EntityNotFoundException.class)); } verify(okrBranchRepository).findByIdOrThrow(id); }
@Test public void update_EntityShouldHaveBeenUpdated() { OkrBranch updatedEntity = new OkrBranch(); updatedEntity.setId(okrBranchId); updatedEntity.setName("s"); updatedEntity.setObjectives(Arrays.asList(new Objective(), new Objective())); updatedEntity.setOkrChildUnits(Arrays.asList(new OkrDepartment(), new OkrDepartment())); updatedEntity.setParentOkrUnit(new OkrDepartment()); OkrBranch actual = okrBranchService.update(okrBranchId, updatedEntity, mockedUser); Assert.assertEquals(updatedEntity, actual); verify(okrBranchRepository).findByIdOrThrow(anyLong()); verify(okrBranchRepository).save(any()); }
@Test(expected = IdDeviationException.class) public void update_EntityIdAndIdAreDifferentShouldThrowIdDeviationException() { OkrBranch updatedEntity = new OkrBranch(); updatedEntity.setId(444L); okrBranchService.update(okrBranchId, updatedEntity, mockedUser); }