RuleEntityServiceImpl implements RuleEntityService { @Override public RuleEntity update(final RuleDTO ruleDTO, final Long id) throws NoSuchElementException { final RuleEntity existingRule = ofNullable(ruleEntityRepository.findOne(id)) .orElseThrow(() -> new NoSuchElementException(format("No such Rule! Id: %s", id))); invalidateRule(existingRule); return save(ruleDTO); }  @Override RuleEntity save(final RuleDTO ruleDTO); @Override RuleEntity update(final RuleDTO ruleDTO, final Long id); @Override RuleEntity findById(final Long id); @Override List<RuleEntity> findByNotExpired(); @Override List<RuleEntity> findAll(); @Override void expire(final Long id, final DateTime newExpiryDate);  }
@Test public void testUpdate() throws Exception { final RuleDTO ruleDTO = new RuleDTO(); ruleDTO.setAccountId("5678"); when(ruleEntityRepository.findOne(anyLong())).thenReturn(ruleEntity); when(ruleEntityRepository.save(any(RuleEntity.class))).then(AdditionalAnswers.returnsFirstArg()); final RuleEntity updatedRule = ruleEntityServiceImpl.update(ruleDTO, 1L); assertThat(updatedRule.getAccountId()).isEqualToIgnoringCase("5678"); assertThat(updatedRule.getExpiryDate()).isEqualByComparingTo(new DateTime(9999, 1, 1, 0, 0, 0, UTC)); verify(ruleEntityRepository).findOne(anyLong()); verify(ruleEntityRepository, times(2)).save(any(RuleEntity.class)); }
@Test(expected = NoSuchElementException.class) public void testUpdateFails() throws Exception { final RuleDTO ruleDTO = new RuleDTO(); ruleDTO.setAccountId("5678"); when(ruleEntityRepository.findOne(anyLong())).thenReturn(null); try { final RuleEntity updatedRule = ruleEntityServiceImpl.update(ruleDTO, 1L); } finally { verify(ruleEntityRepository).findOne(anyLong()); } }