Skip to content

Commit

Permalink
Add warning for skipped fields
Browse files Browse the repository at this point in the history
Issue #468
  • Loading branch information
astubbs authored and fmbenhassine committed Sep 9, 2023
1 parent 6838395 commit 51150d4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<objenesis.version>3.3</objenesis.version>
<datafaker.version>2.0.1</datafaker.version>
<classgraph.version>4.8.160</classgraph.version>
<slf4j.version>2.0.7</slf4j.version>

<!-- test dependencies -->
<junit.version>5.9.3</junit.version>
Expand Down Expand Up @@ -97,6 +98,11 @@
<artifactId>datafaker</artifactId>
<version>${datafaker.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -121,7 +127,13 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jeasy/random/FieldPopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.jeasy.random.api.Randomizer;
import org.jeasy.random.api.RandomizerProvider;
import org.jeasy.random.randomizers.misc.SkipRandomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -52,6 +54,8 @@
*/
class FieldPopulator {

private static final Logger logger = LoggerFactory.getLogger(FieldPopulator.class);

private final EasyRandom easyRandom;

private final ArrayPopulator arrayPopulator;
Expand Down Expand Up @@ -109,6 +113,9 @@ void populateField(final Object target, final Field field, final RandomizationCo
throw new ObjectCreationException(exceptionMessage, e.getCause());
}
}
} else {
logger.warn("Skipping populating field {}#{} as the randomization depth has been reached: {}",
field.getDeclaringClass().getSimpleName(), field.getName(), context.getParameters().getRandomizationDepth());
}
context.popStackItem();
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/jeasy/random/FieldPopulatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ void whenRandomizationDepthIsExceeded_thenFieldsAreNotInitialized() throws Excep
// Given
Field name = Human.class.getDeclaredField("name");
Human human = new Human();
EasyRandomParameters parameters = new EasyRandomParameters().randomizationDepth(5);
RandomizationContext context = Mockito.mock(RandomizationContext.class);
when(context.getParameters()).thenReturn(parameters);
when(context.hasExceededRandomizationDepth()).thenReturn(true);

// When
Expand Down

0 comments on commit 51150d4

Please sign in to comment.