From d29ae4b07f75a5af6fd2d1f3bd743160d3d446eb Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 11 Jan 2021 11:49:21 +0100 Subject: [PATCH] Find Bean Classes In '@Inject'-ed Instance Field Fixes #115 --- junit5/README.md | 2 +- .../jboss/weld/junit5/auto/ClassScanning.java | 17 +++++++++++- .../weld/junit5/auto/InjectInstanceTest.java | 26 +++++++++++++++++++ .../auto/InjectParameterizedInstanceTest.java | 26 +++++++++++++++++++ .../junit5/auto/InjectParameterizedTest.java | 25 ++++++++++++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/auto/InjectInstanceTest.java create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedInstanceTest.java create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/auto/InjectParameterizedTest.java diff --git a/junit5/README.md b/junit5/README.md index 2fafb431..6cb28a81 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 5ed52a45..7f104a6f 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 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 @@ static void scanForRequiredBeanClasses(List> testClasses, Weld weld, bo .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 @@ private static Optional> findFirstAnnotatedConstructor(Class c 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; + } + } 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 00000000..ee09c7e6 --- /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 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 00000000..5066bdff --- /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 { + } + + @Inject + Instance> 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 00000000..023e8143 --- /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 { + } + + @Inject + Foo foo; + + @Test + void test() { + assertNotNull(foo); + } + +}