Skip to content

Commit

Permalink
fix(extensions): inject ZPT fields into superclasses
Browse files Browse the repository at this point in the history
The extensions should inject the engine, client and recordstream into the fields that are available. Before this only tried to inject them into the annotated class. This does not work when inheritance is used and the fields are defined on the superclass of the annotated class.

This commit changes the extension so that it will first look on the annotated class. If it cannot find any of the annotations here, it will work it's way up to the superclass and so on until it either found a field it is able to inject, or no fields at all.
  • Loading branch information
remcowesterhoud committed Dec 14, 2022
1 parent db97f19 commit b912ba9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable

private void injectFields(final ExtensionContext extensionContext, final Object... objects) {
final Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
final Field[] declaredFields = requiredTestClass.getDeclaredFields();
for (final Object object : objects) {
final Optional<Field> field = getField(declaredFields, object);
final Optional<Field> field = getField(requiredTestClass, object);
field.ifPresent(value -> injectField(extensionContext, value, object));
}
}

private Optional<Field> getField(final Field[] declaredFields, final Object object) {
private Optional<Field> getField(final Class<?> requiredTestClass, final Object object) {
final Field[] declaredFields = requiredTestClass.getDeclaredFields();

final List<Field> fields =
Arrays.stream(declaredFields)
.filter(field -> field.getType().isInstance(object))
Expand All @@ -143,8 +144,12 @@ private Optional<Field> getField(final Field[] declaredFields, final Object obje
+ "found %s. Please make sure at most one field of type %s has been declared in the"
+ " test class.",
object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName()));
} else if (fields.size() == 0) {
final Class<?> superclass = requiredTestClass.getSuperclass();
return superclass == null ? Optional.empty() : getField(superclass, object);
} else {
return Optional.of(fields.get(0));
}
return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0));
}

private void injectField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable

private void injectFields(final ExtensionContext extensionContext, final Object... objects) {
final Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
final Field[] declaredFields = requiredTestClass.getDeclaredFields();
for (final Object object : objects) {
final Optional<Field> field = getField(declaredFields, object);
final Optional<Field> field = getField(requiredTestClass, object);
field.ifPresent(value -> injectField(extensionContext, value, object));
}
}

private Optional<Field> getField(final Field[] declaredFields, final Object object) {
private Optional<Field> getField(final Class<?> requiredTestClass, final Object object) {
final Field[] declaredFields = requiredTestClass.getDeclaredFields();

final List<Field> fields =
Arrays.stream(declaredFields)
.filter(field -> field.getType().isInstance(object))
Expand All @@ -119,8 +120,12 @@ private Optional<Field> getField(final Field[] declaredFields, final Object obje
+ "found %s. Please make sure at most one field of type %s has been declared in the"
+ " test class.",
object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName()));
} else if (fields.size() == 0) {
final Class<?> superclass = requiredTestClass.getSuperclass();
return superclass == null ? Optional.empty() : getField(superclass, object);
} else {
return Optional.of(fields.get(0));
}
return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0));
}

private void injectField(
Expand Down

0 comments on commit b912ba9

Please sign in to comment.