Skip to content

Commit

Permalink
pass properly updated context to a ContextAwareRandomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
unconditional authored and fmbenhassine committed Oct 31, 2020
1 parent 8ee6f78 commit 729420b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ void populateField(final Object target, final Field field, final RandomizationCo
if (randomizer instanceof SkipRandomizer) {
return;
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if (randomizer instanceof ContextAwareRandomizer) {
((ContextAwareRandomizer<?>) randomizer).setRandomizerContext(context);
}
context.pushStackItem(new RandomizationContextStackItem(target, field));
if(!context.hasExceededRandomizationDepth()) {
Object value;
if (randomizer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
Expand All @@ -35,6 +36,7 @@

import javax.xml.bind.JAXBElement;

import org.jeasy.random.api.ContextAwareRandomizer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -64,6 +66,8 @@ class FieldPopulatorTest {
@Mock
private Randomizer randomizer;
@Mock
private ContextAwareRandomizer contextAwareRandomizer;
@Mock
private ArrayPopulator arrayPopulator;
@Mock
private CollectionPopulator collectionPopulator;
Expand Down Expand Up @@ -111,6 +115,27 @@ void whenCustomRandomizerIsRegisteredForTheField_thenTheFieldShouldBePopulatedWi
assertThat(human.getName()).isEqualTo(NAME);
}

@Test
void whenContextAwareRandomizerIsRegisteredForTheField_thenTheFieldShouldBeOnTopOfTheSuppliedContextStack() throws Exception {
// Given
Field name = Human.class.getDeclaredField("name");
Human human = new Human();
RandomizationContext context = new RandomizationContext(Human.class, new EasyRandomParameters());
final Human[] currentObjectFromContext = new Human[1];
when(randomizerProvider.getRandomizerByField(name, context)).thenReturn(contextAwareRandomizer);
when(contextAwareRandomizer.getRandomValue()).thenReturn(NAME);
doAnswer(invocationOnMock -> {
currentObjectFromContext[0] = (Human)invocationOnMock.getArgument(0, RandomizationContext.class).getCurrentObject();
return null;
}).when(contextAwareRandomizer).setRandomizerContext(context);

// When
fieldPopulator.populateField(human, name, context);

// Then
assertThat(currentObjectFromContext[0]).isEqualTo(human);
}

@Test
void whenTheFieldIsOfTypeArray_thenShouldDelegatePopulationToArrayPopulator() throws Exception {
// Given
Expand Down

0 comments on commit 729420b

Please sign in to comment.