@Test @Disabled("Objenesis is able to create an instance of JAXBElement type. Hence no error is thrown as expected in this test") void shouldFailWithNiceErrorMessageWhenUnableToCreateFieldValue() throws Exception { FieldPopulator fieldPopulator = new FieldPopulator(new EasyRandom(), randomizerProvider, arrayPopulator, collectionPopulator, mapPopulator); Field jaxbElementField = JaxbElementFieldBean.class.getDeclaredField("jaxbElementField"); JaxbElementFieldBean jaxbElementFieldBean = new JaxbElementFieldBean(); RandomizationContext context = Mockito.mock(RandomizationContext.class); thenThrownBy(() -> { fieldPopulator.populateField(jaxbElementFieldBean, jaxbElementField, context); }) .hasMessage("Unable to create type: javax.xml.bind.JAXBElement for field: jaxbElementField of class: org.jeasy.random.FieldPopulatorTest$JaxbElementFieldBean"); }
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);   }