Skip to content

Commit

Permalink
Added logic to handle static fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasYOY committed Aug 8, 2022
1 parent f94a9c3 commit d82c53c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import io.github.benas.randombeans.api.EnhancedRandom;
import org.junit.jupiter.api.extension.*;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -205,7 +207,8 @@ public Object resolveParameter(
}

/**
* Inject random values into any fields which are annotated with {@link Random}
* Inject random values into any fields which are annotated with {@link Random}.
* This method doesn't populate static fields if they have values.
*
* @param testInstance the instance to post-process
* @param extensionContext the current extension context
Expand All @@ -218,9 +221,11 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext extens
if (isAnnotated(field, Random.class)) {
Random annotation = field.getAnnotation(Random.class);
Object randomObject = resolve(field.getType(), annotation);

field.setAccessible(true);
field.set(testInstance, randomObject);
if (!Modifier.isStatic(field.getModifiers()) || field.get(testInstance) == null) {
field.set(testInstance, randomObject);
}
}
}
}
Expand Down Expand Up @@ -249,4 +254,5 @@ private Object resolve(Class<?> targetType, Random annotation) {
return random.nextObject(targetType, annotation.excludes());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
@ExtendWith(RandomBeansExtension.class)
public class RandomBeansExtensionFieldTest {

@Random private static String anyStaticString;

// gather the random values to facilitate assertions on the not distinct-ness of the value supplied to
// each test
private static Collection<String> staticStringCollection = new HashSet<>();

// gather the random values to facilitate assertions on the distinct-ness of the value supplied to
// each test
private final Set<String> anyStrings = new HashSet<>();
Expand Down Expand Up @@ -80,6 +86,11 @@ public void canInjectAFullyPopulatedRandomObject() {
assertThatDomainObjectIsFullyPopulated(fullyPopulatedDomainObject);
}

@Test
public void acnInjectStaticFields() {
assertThat(anyStaticString, is(notNullValue()));
}

@Test
public void canInjectAPartiallyPopulatedRandomObject() {
assertThatDomainObjectIsPartiallyPopulated(partiallyPopulatedDomainObject);
Expand Down Expand Up @@ -148,4 +159,11 @@ public void willInjectANewRandomValueEachTime() {
anyStrings.add(anyString);
}
}

@RepeatedTest(5)
public void willNotInjectANewRandomValueEachTime() {
assertThat(anyStaticString, notNullValue());
staticStringCollection.add(anyStaticString);
assertThat(staticStringCollection, is(hasSize(1)));
}
}

0 comments on commit d82c53c

Please sign in to comment.