Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
glytching committed Aug 9, 2022
2 parents 9756813 + 9652a90 commit 2b1d26e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
15 changes: 15 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
jobs:
build:
docker:
- image: circleci/openjdk:8-jdk

working_directory: ~/repo

environment:
TERM: dumb

steps:
- checkout

- run: mvn verify jacoco:report coveralls:report -DrepoToken=w7KlgSEVtzzJRRCxI84aCx3kwKOy0DR0r
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions docs/randomBeans.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ This extension is engaged by adding the `@Random` annotation to a test class fie
- `size`: for collection types, the size of the generated collection
- `type`: for collection types, the underlying type of a generic collection

You can use `@Random` annotation to a `static` field.
From **v2.6.0** `static` field will be populated only once. Note, that in case of any default value (except `null`) the value will not be overridden by the extension.

#### Examples

###### Test Class Fields
Expand All @@ -23,6 +26,8 @@ public class MyTest {
// injected with a random String
@Random private String anyString;
@Random private static String anyStaticString;
// injected with a random, fully populated instance of DomainObject
@Random private DomainObject fullyPopulatedDomainObject;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JUnit Extensions
====

[![Build Status](https://travis-ci.org/glytching/junit-extensions.svg?branch=master)](https://travis-ci.org/glytching/junit-extensions) [![Coverage Status](https://coveralls.io/repos/github/glytching/junit-extensions/badge.svg?branch=master)](https://coveralls.io/github/glytching/junit-extensions?branch=master) [![Scrutinizer](https://img.shields.io/scrutinizer/g/glytching/junit-extensions.svg)](https://scrutinizer-ci.com/g/glytching/junit-extensions/) [![Javadoc](https://javadoc.io/badge2/io.github.glytching/junit-extensions/javadoc.svg)](https://javadoc.io/doc/io.github.glytching/junit-extensions) [![Maven Central](https://img.shields.io/maven-central/v/io.github.glytching/junit-extensions.svg)](https://repo1.maven.org/maven2/io/github/glytching/junit-extensions/2.5.0/) [![GitHub Release](https://img.shields.io/github/release/glytching/junit-extensions.svg)](https://github.com/glytching/junit-extensions/releases)
[![Build Status](https://circleci.com/gh/glytching/junit-extensions.svg?style=svg)](https://circleci.com/gh/glytching/junit-extensions) [![Coverage Status](https://coveralls.io/repos/github/glytching/junit-extensions/badge.svg?branch=master)](https://coveralls.io/github/glytching/junit-extensions?branch=master) [![Scrutinizer](https://img.shields.io/scrutinizer/g/glytching/junit-extensions.svg)](https://scrutinizer-ci.com/g/glytching/junit-extensions/) [![Javadoc](https://javadoc.io/badge2/io.github.glytching/junit-extensions/javadoc.svg)](https://javadoc.io/doc/io.github.glytching/junit-extensions) [![Maven Central](https://img.shields.io/maven-central/v/io.github.glytching/junit-extensions.svg)](https://repo1.maven.org/maven2/io/github/glytching/junit-extensions/2.5.0/) [![GitHub Release](https://img.shields.io/github/release/glytching/junit-extensions.svg)](https://github.com/glytching/junit-extensions/releases)

> With thanks and appreciation to the authors of [JUnit5](https://github.com/junit-team/junit5/graphs/contributors).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.extension.*;

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 +206,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 +220,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 +253,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> anyStaticStrings = 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 canInjectStaticFields() {
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 willNotInjectANewRandomValueEachTimeForAStaticField() {
assertThat(anyStaticString, notNullValue());
anyStaticStrings.add(anyStaticString);
assertThat(anyStaticStrings, is(hasSize(1)));
}
}

0 comments on commit 2b1d26e

Please sign in to comment.