Repository: weld/weld-junit
Pull request #113 (2021-01-08 11:32:12)

Find Bean Classes In '@Inject'-ed Instance<BeanType> Field
Weld injects Instance<T> fields and therefore I figure that `@EnableAutoWeld` should similarly detect these types and automatically add them as bean classes to Weld.

philippkunz: I'm afraid I have not found out how to properly link this pull request with the issue. In the right hand pane it shows:

> Linked issues
> 
> Successfully merging this pull request may close these issues.
> 
> None yet
manovotn: It should be linked. You can achieve that by either typing fixes #115  in either comments or the commit message (and there is more variants than just "fixes" that you can use).
Here is a [GH documentation](https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue) regarding this.

Patch:
diff --git a/junit5/README.md b/junit5/README.md
index 2fafb43..6cb28a8 100644
--- a/junit5/README.md
+++ b/junit5/README.md
@@ -385,7 +385,7 @@ class MyTest {
 
 ## WeldJunit5AutoExtension
 
-To use this approach, annotate your test class with `ExtendWith(WeldJunit5AutoExtension.class)` or just `@EnableAutoWeld`.
+To use this approach, annotate your test class with `@ExtendWith(WeldJunit5AutoExtension.class)` or just `@EnableAutoWeld`.
 By default, the extension will:
 
 * Inspect your test class and try to figure out what bean classes it needs based on injection points (field and parameter injection both work)
diff --git a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
index 5ed52a4..7f104a6 100644
--- a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
+++ b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
@@ -31,6 +31,7 @@ import org.junit.platform.commons.util.Preconditions;
 import javax.decorator.Decorator;
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.NormalScope;
+import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.Stereotype;
 import javax.enterprise.inject.spi.Extension;
@@ -95,7 +96,7 @@ class ClassScanning {
                     .forEach(excludedBeanTypes::add);
 
             AnnotationSupport.findAnnotatedFields(currClass, Inject.class).stream()
-                    .map(Field::getType)
+                    .map(ClassScanning::unwrapInstanceTypeParameter)
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, Inject.class, HierarchyTraversalMode.BOTTOM_UP).stream()
@@ -330,4 +331,18 @@ class ClassScanning {
         return findFirstAnnotatedConstructor(clazz.getSuperclass(), annotationType);
     }
 
+    private static Class<?> unwrapInstanceTypeParameter(Field field) {
+        Class<?> type = field.getType();
+        if (type.equals(Instance.class)) {
+            ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
+            Type typeParameter = parameterizedType.getActualTypeArguments()[0];
+            if (typeParameter instanceof ParameterizedType) {
+                type = (Class<?>) ((ParameterizedType) typeParameter).getRawType();
+            } else {
+                type = (Class<?>) typeParameter;
+            }
+        }
+        return type;
+    }
+
 }


Test patch:
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectInstanceTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectInstanceTest.java
new file mode 100644
index 0000000..ee09c7e
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectInstanceTest.java
@@ -0,0 +1,26 @@
+package org.jboss.weld.junit5.auto;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+import org.junit.jupiter.api.Test;
+
+@EnableAutoWeld
+public class InjectInstanceTest {
+
+    @Dependent
+    static class Foo {
+    }
+
+    @Inject
+    Instance<Foo> fooInstance;
+
+    @Test
+    void test() {
+        assertNotNull(fooInstance.get());
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedInstanceTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedInstanceTest.java
new file mode 100644
index 0000000..5066bdf
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedInstanceTest.java
@@ -0,0 +1,26 @@
+package org.jboss.weld.junit5.auto;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+import org.junit.jupiter.api.Test;
+
+@EnableAutoWeld
+public class InjectParameterizedInstanceTest {
+
+    @Dependent
+    static class Foo<T> {
+    }
+
+    @Inject
+    Instance<Foo<String>> fooInstance;
+
+    @Test
+    void test() {
+        assertNotNull(fooInstance.get());
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedTest.java
new file mode 100644
index 0000000..023e814
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedTest.java
@@ -0,0 +1,25 @@
+package org.jboss.weld.junit5.auto;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import javax.enterprise.context.Dependent;
+import javax.inject.Inject;
+
+import org.junit.jupiter.api.Test;
+
+@EnableAutoWeld
+public class InjectParameterizedTest {
+
+    @Dependent
+    static class Foo<T> {
+    }
+
+    @Inject
+    Foo<String> foo;
+
+    @Test
+    void test() {
+        assertNotNull(foo);
+    }
+
+}