@Test void whenRandomizationDepthIsExceeded_thenFieldsAreNotInitialized() throws Exception { Field name = Human.class.getDeclaredField("name"); Human human = new Human(); RandomizationContext context = Mockito.mock(RandomizationContext.class); when(context.hasExceededRandomizationDepth()).thenReturn(true); fieldPopulator.populateField(human, name, context); assertThat(human.getName()).isNull(); }
void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { Randomizer<?> randomizer = getRandomizer(field, context); if (randomizer instanceof SkipRandomizer) { return; } if (randomizer instanceof ContextAwareRandomizer) { ((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context); } context.pushStackItem(new RandomizationContextStackItem(target, field)); if(!context.hasExceededRandomizationDepth()) { Object value; if (randomizer != null) { value = randomizer.getRandomValue(); } else { try { value = generateRandomValue(field, context); } catch (ObjectCreationException e) { String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s", field.getType().getName(), field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e); } } if (context.getParameters().isBypassSetters()) { setFieldValue(target, field, value); } else { try { setProperty(target, field, value); } catch (InvocationTargetException e) { String exceptionMessage = String.format("Unable to invoke setter for field %s of class %s", field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e.getCause()); } } } context.popStackItem(); }
FieldPopulator { void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { Randomizer<?> randomizer = getRandomizer(field, context); if (randomizer instanceof SkipRandomizer) { return; } if (randomizer instanceof ContextAwareRandomizer) { ((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context); } context.pushStackItem(new RandomizationContextStackItem(target, field)); if(!context.hasExceededRandomizationDepth()) { Object value; if (randomizer != null) { value = randomizer.getRandomValue(); } else { try { value = generateRandomValue(field, context); } catch (ObjectCreationException e) { String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s", field.getType().getName(), field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e); } } if (context.getParameters().isBypassSetters()) { setFieldValue(target, field, value); } else { try { setProperty(target, field, value); } catch (InvocationTargetException e) { String exceptionMessage = String.format("Unable to invoke setter for field %s of class %s", field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e.getCause()); } } } context.popStackItem(); } }
FieldPopulator { void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { Randomizer<?> randomizer = getRandomizer(field, context); if (randomizer instanceof SkipRandomizer) { return; } if (randomizer instanceof ContextAwareRandomizer) { ((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context); } context.pushStackItem(new RandomizationContextStackItem(target, field)); if(!context.hasExceededRandomizationDepth()) { Object value; if (randomizer != null) { value = randomizer.getRandomValue(); } else { try { value = generateRandomValue(field, context); } catch (ObjectCreationException e) { String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s", field.getType().getName(), field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e); } } if (context.getParameters().isBypassSetters()) { setFieldValue(target, field, value); } else { try { setProperty(target, field, value); } catch (InvocationTargetException e) { String exceptionMessage = String.format("Unable to invoke setter for field %s of class %s", field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e.getCause()); } } } context.popStackItem(); } FieldPopulator(final EasyRandom easyRandom, final RandomizerProvider randomizerProvider,
                   final ArrayPopulator arrayPopulator, final CollectionPopulator collectionPopulator, final MapPopulator mapPopulator); }
FieldPopulator { void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { Randomizer<?> randomizer = getRandomizer(field, context); if (randomizer instanceof SkipRandomizer) { return; } if (randomizer instanceof ContextAwareRandomizer) { ((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context); } context.pushStackItem(new RandomizationContextStackItem(target, field)); if(!context.hasExceededRandomizationDepth()) { Object value; if (randomizer != null) { value = randomizer.getRandomValue(); } else { try { value = generateRandomValue(field, context); } catch (ObjectCreationException e) { String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s", field.getType().getName(), field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e); } } if (context.getParameters().isBypassSetters()) { setFieldValue(target, field, value); } else { try { setProperty(target, field, value); } catch (InvocationTargetException e) { String exceptionMessage = String.format("Unable to invoke setter for field %s of class %s", field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e.getCause()); } } } context.popStackItem(); } FieldPopulator(final EasyRandom easyRandom, final RandomizerProvider randomizerProvider,
                   final ArrayPopulator arrayPopulator, final CollectionPopulator collectionPopulator, final MapPopulator mapPopulator);  }
FieldPopulator { void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { Randomizer<?> randomizer = getRandomizer(field, context); if (randomizer instanceof SkipRandomizer) { return; } if (randomizer instanceof ContextAwareRandomizer) { ((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context); } context.pushStackItem(new RandomizationContextStackItem(target, field)); if(!context.hasExceededRandomizationDepth()) { Object value; if (randomizer != null) { value = randomizer.getRandomValue(); } else { try { value = generateRandomValue(field, context); } catch (ObjectCreationException e) { String exceptionMessage = String.format("Unable to create type: %s for field: %s of class: %s", field.getType().getName(), field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e); } } if (context.getParameters().isBypassSetters()) { setFieldValue(target, field, value); } else { try { setProperty(target, field, value); } catch (InvocationTargetException e) { String exceptionMessage = String.format("Unable to invoke setter for field %s of class %s", field.getName(), target.getClass().getName()); throw new ObjectCreationException(exceptionMessage, e.getCause()); } } } context.popStackItem(); } FieldPopulator(final EasyRandom easyRandom, final RandomizerProvider randomizerProvider,
                   final ArrayPopulator arrayPopulator, final CollectionPopulator collectionPopulator, final MapPopulator mapPopulator);   }