### Question:
FlagService { public void updateFlag(Flag flag) throws FlagNotFoundException, FlagWithoutIdException { if (flag.getFlagId() == null) { throw new FlagWithoutIdException(flag.getName()); } getFlag(flag.getFlagId()); flagRepository.save(flag); } @Autowired  FlagService(FlagRepository flagRepository); List<Flag> getFlags(); Flag getFlag(Long id); Flag addFlag(Flag flag); void removeFlag(Long id); void updateFlag(Flag flag);  }### Answer:
@Test public void cannotUpdateFlagThatDoesNotExist() { final Long id = randomLong(); when(repository.findOne(id)).thenReturn(null); final Flag updatedFlag = new Flag(id, randomString(), randomInt(100), false); expectException(() -> flagService.updateFlag(updatedFlag), FlagNotFoundException.class, id.toString(), "successfully updated"); }
@Test public void cannotUpdateFlagWithoutId() { final String flagName = randomString(); final Flag updatedFlag = new Flag(null, flagName, randomInt(100), false); expectException(() -> flagService.updateFlag(updatedFlag), FlagWithoutIdException.class, flagName, "successfully updated"); }
@Test public void canUpdateFlagIfDoneCorrectly() throws FlagWithoutIdException, FlagNotFoundException { final long flagId = randomLong(); final Flag originalFlag = new Flag(flagId, randomString(), randomInt(100), false); when(repository.findOne(flagId)).thenReturn(originalFlag); final Flag newFlag = new Flag(flagId, randomString(), randomInt(100), randomBoolean()); flagService.updateFlag(newFlag); verify(repository, once()).save(newFlag); }