Skip to content

Commit d254434

Browse files
authored
Merge pull request #51 from webcompere/resolve-junit-private-dependency
Remove dependency on private JUnit API internals
2 parents 93db7a1 + b1f9d2b commit d254434

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

system-stubs-junit4/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project and has been rebuilt from the ground up for this version.
99
<dependency>
1010
<groupId>uk.org.webcompere</groupId>
1111
<artifactId>system-stubs-junit4</artifactId>
12-
<version>1.2.0</version>
12+
<version>2.0.1</version>
1313
</dependency>
1414
```
1515

@@ -250,4 +250,4 @@ other situations, and need to make these work in JUnit 4 also.
250250

251251
There are extensive tests demonstrating each of the JUnit rules. However, in some situations, you may need finer control of the System Stub objects. You can always mix using rules and core objects via the `execute` pattern. It may also be easier to combine methods from `SystemStubs` with some common rules.
252252

253-
It is worth noting that putting common set up into a `@Rule` object is useful unless there is nothing common between test cases. In that instance, consider having multiple test classes for each configuration, or using only the most essential common setup within rules, and adding the `execute` pattern for variations.
253+
It is worth noting that putting common set up into a `@Rule` object is useful unless there is nothing common between test cases. In that instance, consider having multiple test classes for each configuration, or using only the most essential common setup within rules, and adding the `execute` pattern for variations.

system-stubs-jupiter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ JUnit 5 unit test.
77
<dependency>
88
<groupId>uk.org.webcompere</groupId>
99
<artifactId>system-stubs-jupiter</artifactId>
10-
<version>1.2.0</version>
10+
<version>2.0.1</version>
1111
</dependency>
1212
```
1313

system-stubs-jupiter/src/main/java/uk/org/webcompere/systemstubs/jupiter/SystemStubsExtension.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package uk.org.webcompere.systemstubs.jupiter;
22

33
import org.junit.jupiter.api.extension.*;
4+
import org.junit.platform.commons.function.Try;
45
import uk.org.webcompere.systemstubs.resource.TestResource;
56

7+
import java.lang.reflect.AccessibleObject;
68
import java.lang.reflect.Field;
79
import java.util.*;
810
import java.util.function.Predicate;
911

1012
import static java.lang.reflect.Modifier.isStatic;
11-
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields;
12-
import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible;
13-
import static org.junit.platform.commons.util.ReflectionUtils.tryToReadFieldValue;
13+
import static org.junit.platform.commons.support.HierarchyTraversalMode.TOP_DOWN;
14+
import static org.junit.platform.commons.support.ReflectionSupport.findFields;
1415
import static uk.org.webcompere.systemstubs.resource.Resources.executeCleanup;
1516

1617
/**
@@ -49,7 +50,7 @@ public Object resolveParameter(ParameterContext parameterContext,
4950
ExtensionContext extensionContext) throws ParameterResolutionException {
5051
try {
5152
// create using default constructor, turn it on and remember it for cleanup
52-
TestResource resource = (TestResource)parameterContext.getParameter().getType().newInstance();
53+
TestResource resource = (TestResource) parameterContext.getParameter().getType().newInstance();
5354
resource.setup();
5455

5556
activeResources.addFirst(resource);
@@ -90,14 +91,14 @@ private void setup(Field field, Object testInstance) throws Exception {
9091

9192
private TestResource getInstantiatedTestResource(Field field, Object testInstance) {
9293
return tryToReadFieldValue(field, testInstance)
93-
.toOptional()
94-
.map(val -> (TestResource)val)
95-
.orElseGet(() -> assignNewInstanceToField(field, testInstance));
94+
.toOptional()
95+
.map(val -> (TestResource) val)
96+
.orElseGet(() -> assignNewInstanceToField(field, testInstance));
9697
}
9798

9899
private TestResource assignNewInstanceToField(Field field, Object testInstance) {
99100
try {
100-
TestResource resource = (TestResource)field.getType().newInstance();
101+
TestResource resource = (TestResource) field.getType().newInstance();
101102
field.set(testInstance, resource);
102103
return resource;
103104
} catch (InstantiationException | IllegalAccessException e) {
@@ -106,19 +107,25 @@ private TestResource assignNewInstanceToField(Field field, Object testInstance)
106107
}
107108

108109
private void setupFields(Class<?> clazz, Object testInstance, Predicate<Field> predicate) throws Exception {
109-
for (Field field : findAnnotatedFields(clazz, SystemStub.class, predicate)) {
110+
for (Field field : findSystemStubsFields(clazz, predicate)) {
110111
setup(field, testInstance);
111112
}
112113
}
113114

115+
private List<Field> findSystemStubsFields(Class<?> clazz, Predicate<Field> predicate) {
116+
Predicate<Field> annotated = field -> field.isAnnotationPresent(SystemStub.class);
117+
return findFields(clazz, annotated.and(predicate), TOP_DOWN);
118+
}
119+
114120
private void cleanupFields(Class<?> clazz, Object testInstance, Predicate<Field> predicate) throws Exception {
115121
LinkedList<TestResource> active = new LinkedList<>();
116-
findAnnotatedFields(clazz, SystemStub.class, predicate)
122+
123+
findSystemStubsFields(clazz, predicate)
117124
.stream()
118125
.map(field -> tryToReadFieldValue(field, testInstance).toOptional())
119126
.filter(Optional::isPresent)
120127
.map(Optional::get)
121-
.map(item -> (TestResource)item)
128+
.map(TestResource.class::cast)
122129
.forEach(active::addFirst);
123130

124131
executeCleanup(active);
@@ -133,4 +140,16 @@ private static boolean isStaticField(Field f) {
133140
private static <T> Predicate<T> not(Predicate<T> predicate) {
134141
return predicate.negate();
135142
}
143+
144+
@SuppressWarnings("deprecation") // "AccessibleObject.isAccessible()" is deprecated in Java 9
145+
private static <T extends AccessibleObject> T makeAccessible(T object) {
146+
if (!object.isAccessible()) {
147+
object.setAccessible(true);
148+
}
149+
return object;
150+
}
151+
152+
private static Try<Object> tryToReadFieldValue(Field field, Object instance) {
153+
return Try.call(() -> makeAccessible(field).get(instance));
154+
}
136155
}

0 commit comments

Comments
 (0)