### Question:
EntityServiceImpl implements EntityService { @Override public Object getField(final Object databaseEntity, final FieldDefinition fieldDefinition) { return getField(databaseEntity, fieldDefinition, null); }  @Override Long getId(final Object databaseEntity); Boolean getActive(final Object databaseEntity); void setActive(final Object databaseEntity, final boolean active); @Override void setId(final Object databaseEntity, final Long id); @Override void setField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Object value); @Override Object getField(final Object databaseEntity, final FieldDefinition fieldDefinition); Object getField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer); @Override Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity); Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity,
            final Entity performer); @SuppressWarnings("unchecked") @Override Object convertToDatabaseEntity(final InternalDataDefinition dataDefinition, final Entity genericEntity,
            final Object existingDatabaseEntity);  }### Answer:
@Test(expected = IllegalStateException.class) public void shouldThrownAnExceptionWhileGettingNotExistingField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); FieldDefinition fieldDefinition = new FieldDefinitionImpl(new DataDefinitionImpl("", "", null), "unknown"); entityService.getField(databaseEntity, fieldDefinition); }
@Test public void shouldReturnNullWhileGettingEmptyField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); Object value = entityService.getField(databaseEntity, fieldDefinitionName); assertNull(value); }
@Test public void shouldReturnProperValueOfTheField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); databaseEntity.setName("Mr T"); Object value = entityService.getField(databaseEntity, fieldDefinitionName); assertEquals("Mr T", value); }
@Test public void shouldNotThrownAnExceptionWhileGettingFieldWithInvalidType() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); databaseEntity.setName("Mr T"); FieldDefinition fieldDefinition = new FieldDefinitionImpl(new DataDefinitionImpl("", "", null), "name").withType(new IntegerType()); entityService.getField(databaseEntity, fieldDefinition); }
@Test public void shouldLazyLoadEntitiesUsingProxy() throws Exception { InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS); FieldDefinition fieldDefinition = mock(FieldDefinition.class); BelongsToType fieldType = mock(BelongsToType.class); Entity entity1 = new DefaultEntity(dataDefinition, 1L); Entity entity2 = new DefaultEntity(dataDefinition, 2L); given(fieldDefinition.getName()).willReturn("joinField"); given(fieldDefinition.getType()).willReturn(fieldType); given(fieldType.getDataDefinition()).willReturn(dataDefinition); given(dataDefinition.isEnabled()).willReturn(true); given(dataDefinition.getField("joinField")).willReturn(fieldDefinition); given( dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()) .add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 5L)).list().getEntities()).willReturn( Lists.newArrayList(entity1, entity2)); List<Entity> entityList = new EntityListImpl(dataDefinition, "joinField", 5L); assertNotNull(entityList); assertEquals(2, entityList.size()); assertThat(entityList, JUnitMatchers.hasItems(entity1, entity2)); }