AbstractEntityService { public DE getById(String id) throws EntityNotFoundException { DE entity = gateway.findById(id); if (entity == null) { throw new EntityNotFoundException(domainEntityClass, id); } return entity; } protected  AbstractEntityService(Class<DE> domainEntityClass, G gateway); DE add(AP addPayload); DE getById(String id); List<DE> getAll();  }
@Test public void whenGetByIdWithExistingIdItShouldReturnEntity() throws EntityNotFoundException { when(gateway.findById(TEST_ID)).thenReturn(domainEntity); TestEntity actual = service.getById(TEST_ID); verify(gateway).findById(TEST_ID); verifyNoMoreInteractions(gateway); assertThat(actual).isSameAs(domainEntity); }
@Test public void whenGetByIdWithNotExistingIdItShouldThrowEntityNotFoundExceptionWithDescriptiveMessage() { when(gateway.findById(TEST_ID)).thenReturn(null); try { TestEntity actual = service.getById(TEST_ID); failBecauseExceptionWasNotThrown(EntityNotFoundException.class); } catch (Exception e) { assertThat(e).isInstanceOf(EntityNotFoundException.class); assertThat(e) .hasMessageContaining(TestEntity.class.getSimpleName()) .hasMessageContaining("not found"); } }