Skip to content

Commit 51150d4

Browse files
astubbsfmbenhassine
authored andcommitted
Add warning for skipped fields
Issue #468
1 parent 6838395 commit 51150d4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

pom.xml

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<objenesis.version>3.3</objenesis.version>
2525
<datafaker.version>2.0.1</datafaker.version>
2626
<classgraph.version>4.8.160</classgraph.version>
27+
<slf4j.version>2.0.7</slf4j.version>
2728

2829
<!-- test dependencies -->
2930
<junit.version>5.9.3</junit.version>
@@ -97,6 +98,11 @@
9798
<artifactId>datafaker</artifactId>
9899
<version>${datafaker.version}</version>
99100
</dependency>
101+
<dependency>
102+
<groupId>org.slf4j</groupId>
103+
<artifactId>slf4j-api</artifactId>
104+
<version>${slf4j.version}</version>
105+
</dependency>
100106
<dependency>
101107
<groupId>org.junit.jupiter</groupId>
102108
<artifactId>junit-jupiter</artifactId>
@@ -121,7 +127,13 @@
121127
<version>${mockito.version}</version>
122128
<scope>test</scope>
123129
</dependency>
124-
</dependencies>
130+
<dependency>
131+
<groupId>org.slf4j</groupId>
132+
<artifactId>slf4j-simple</artifactId>
133+
<version>${slf4j.version}</version>
134+
<scope>test</scope>
135+
</dependency>
136+
</dependencies>
125137

126138
<build>
127139
<plugins>

src/main/java/org/jeasy/random/FieldPopulator.java

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.jeasy.random.api.Randomizer;
3232
import org.jeasy.random.api.RandomizerProvider;
3333
import org.jeasy.random.randomizers.misc.SkipRandomizer;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
import java.lang.reflect.Field;
3638
import java.lang.reflect.InvocationTargetException;
@@ -52,6 +54,8 @@
5254
*/
5355
class FieldPopulator {
5456

57+
private static final Logger logger = LoggerFactory.getLogger(FieldPopulator.class);
58+
5559
private final EasyRandom easyRandom;
5660

5761
private final ArrayPopulator arrayPopulator;
@@ -109,6 +113,9 @@ void populateField(final Object target, final Field field, final RandomizationCo
109113
throw new ObjectCreationException(exceptionMessage, e.getCause());
110114
}
111115
}
116+
} else {
117+
logger.warn("Skipping populating field {}#{} as the randomization depth has been reached: {}",
118+
field.getDeclaringClass().getSimpleName(), field.getName(), context.getParameters().getRandomizationDepth());
112119
}
113120
context.popStackItem();
114121
}

src/test/java/org/jeasy/random/FieldPopulatorTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ void whenRandomizationDepthIsExceeded_thenFieldsAreNotInitialized() throws Excep
183183
// Given
184184
Field name = Human.class.getDeclaredField("name");
185185
Human human = new Human();
186+
EasyRandomParameters parameters = new EasyRandomParameters().randomizationDepth(5);
186187
RandomizationContext context = Mockito.mock(RandomizationContext.class);
188+
when(context.getParameters()).thenReturn(parameters);
187189
when(context.hasExceededRandomizationDepth()).thenReturn(true);
188190

189191
// When

0 commit comments

Comments
 (0)