Skip to content

Fix for @InjectTest with inherited field injection #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Holds the global BeanScope used for all tests.
*/
final class GlobalTestScope implements ExtensionContext.Store.CloseableResource {
final class GlobalTestScope implements ExtensionContext.Store.CloseableResource {

private static final System.Logger log = AppLog.getLogger("io.avaje.inject");

Expand Down
20 changes: 17 additions & 3 deletions inject-test/src/main/java/io/avaje/inject/test/MetaReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

final class MetaReader {
Expand All @@ -38,10 +39,23 @@ final class MetaReader {

MetaReader(Class<?> testClass, Plugin plugin) {
this.plugin = plugin;
this.methodFinder = new SetupMethods(testClass);
for (Field field : testClass.getDeclaredFields()) {
readField(field);
final var hierarchy = typeHierarchy(testClass);
this.methodFinder = new SetupMethods(hierarchy);
for (Class<?> aTestClass : hierarchy) {
for (Field field : aTestClass.getDeclaredFields()) {
readField(field);
}
}
}

private static LinkedList<Class<?>> typeHierarchy(Class<?> testClass) {
var hierarchy = new LinkedList<Class<?>>();
var analyzedClass = testClass;
while (analyzedClass != null && !analyzedClass.equals(Object.class)) {
hierarchy.addFirst(analyzedClass);
analyzedClass = analyzedClass.getSuperclass();
}
return hierarchy;
}

boolean hasClassInjection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ final class SetupMethods {
private final List<Method> staticMethods = new ArrayList<>();
private final List<Method> instanceMethods = new ArrayList<>();

SetupMethods(Class<?> testClass) {
var analyzedClass = testClass;
var hierarchy = new LinkedList<Class<?>>();
while (analyzedClass != null && !analyzedClass.equals(Object.class)) {
hierarchy.addFirst(analyzedClass);
analyzedClass = analyzedClass.getSuperclass();
}
SetupMethods(LinkedList<Class<?>> hierarchy) {
for (Class<?> aClass : hierarchy) {
for (Method method : aClass.getDeclaredMethods()) {
if (method.getDeclaredAnnotation(Setup.class) != null) {
Expand Down
10 changes: 10 additions & 0 deletions inject-test/src/test/java/org/example/inherit/MyOneAbstract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.example.inherit;

import jakarta.inject.Inject;
import org.example.coffee.core.Steamer;

public abstract class MyOneAbstract {

@Inject
Steamer steamer;
}
22 changes: 22 additions & 0 deletions inject-test/src/test/java/org/example/inherit/MyOneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.example.inherit;

import io.avaje.inject.test.InjectExtension;
import jakarta.inject.Inject;
import org.example.inherit.notpublic.PubExposed;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(InjectExtension.class)
public class MyOneTest extends MyOneAbstract {

@Inject
PubExposed something;

@Test
void test_expect_inheritedFieldIsInjected() {
assertThat(something).isNotNull();
assertThat(steamer).isNotNull();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This [inherited] steamer field was not getting injected

}
}