Repository: weld/weld-junit (java)

Issue:
Injection problem in test class for contextual instances
If you try to inject an instance of a requestscoped bean in a test class with @Inject annotation, it fails with an error: context not active exception.

Possible solution:
Looking at code I have made a change in  AbstractWeldInitiator.initWeldContainer, swapping injectInstances with the next 3 lines of code (extension.activateContexts) and it works well. Now I can inject a contextual instance without problems.

Best regards
Jacopo

Pull request:
Activate contexts prior to injection into test instances.
Fixes #93 

Patch:
diff --git a/README.md b/README.md
index dbbb206..567cc85 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@
 
 The primary goal of this project is to provide simple and fast tools for CDI *unit/component* testing.
 The tools are implemented as JUnit 4 and JUnit 5 extensions.
-Supports Weld **2.4** (CDI 1.2) and **3.0** (CDI 2.0).
+Supports **3.0** (CDI 2.0).
 
 ## The What
 
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/AbstractWeldInitiator.java b/junit-common/src/main/java/org/jboss/weld/junit/AbstractWeldInitiator.java
index 9cdbfbe..a6a4208 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/AbstractWeldInitiator.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/AbstractWeldInitiator.java
@@ -500,10 +500,10 @@ public abstract class AbstractWeldInitiator implements Instance<Object>, Contain
         }
         // Init the container
         container = weld.initialize();
-        injectInstances();
         if (extension != null) {
             extension.activateContexts();
         }
+        injectInstances();
         return container;
     }
 


Test patch:
diff --git a/junit4/src/test/java/org/jboss/weld/junit4/contexts/ContextsActivatedTest.java b/junit4/src/test/java/org/jboss/weld/junit4/contexts/ContextsActivatedTest.java
index 8dcabe5..12f0b63 100644
--- a/junit4/src/test/java/org/jboss/weld/junit4/contexts/ContextsActivatedTest.java
+++ b/junit4/src/test/java/org/jboss/weld/junit4/contexts/ContextsActivatedTest.java
@@ -17,9 +17,11 @@
 package org.jboss.weld.junit4.contexts;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
 
 import org.jboss.weld.junit4.WeldInitiator;
 import org.junit.Rule;
@@ -32,13 +34,19 @@ import org.junit.Test;
 public class ContextsActivatedTest {
 
     @Rule
-    public WeldInitiator weld = WeldInitiator.from(Foo.class, Oof.class)
+    public WeldInitiator weld = WeldInitiator.from(Foo.class, Oof.class, RequestScopedProducer.class)
+            .inject(this)
             .activate(RequestScoped.class, SessionScoped.class).build();
 
+    @Inject
+    String producedString;
+
     @Test
     public void testNormalScopes() {
         assertEquals(weld.select(Foo.class).get().getId(), weld.select(Foo.class).get().getId());
         assertEquals(weld.select(Oof.class).get().getId(), weld.select(Oof.class).get().getId());
+        assertNotNull(producedString);
+        assertEquals("foo", producedString);
     }
 
 }
diff --git a/junit4/src/test/java/org/jboss/weld/junit4/contexts/RequestScopedProducer.java b/junit4/src/test/java/org/jboss/weld/junit4/contexts/RequestScopedProducer.java
new file mode 100644
index 0000000..91a8f95
--- /dev/null
+++ b/junit4/src/test/java/org/jboss/weld/junit4/contexts/RequestScopedProducer.java
@@ -0,0 +1,15 @@
+package org.jboss.weld.junit4.contexts;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Produces;
+
+@RequestScoped
+public class RequestScopedProducer {
+
+    @Produces
+    @Dependent
+    public String produceDependentString() {
+        return "foo";
+    }
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/contexts/ContextsActivatedTest.java b/junit5/src/test/java/org/jboss/weld/junit5/contexts/ContextsActivatedTest.java
index c11b5ff..42635ab 100644
--- a/junit5/src/test/java/org/jboss/weld/junit5/contexts/ContextsActivatedTest.java
+++ b/junit5/src/test/java/org/jboss/weld/junit5/contexts/ContextsActivatedTest.java
@@ -18,6 +18,7 @@ package org.jboss.weld.junit5.contexts;
 
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
 
 import org.jboss.weld.junit5.WeldInitiator;
 import org.jboss.weld.junit5.WeldJunit5Extension;
@@ -35,13 +36,18 @@ import org.junit.jupiter.api.extension.ExtendWith;
 public class ContextsActivatedTest {
 
     @WeldSetup
-    public WeldInitiator weld = WeldInitiator.from(Foo.class, Oof.class)
+    public WeldInitiator weld = WeldInitiator.from(Foo.class, Oof.class, RequestScopedProducer.class)
             .activate(RequestScoped.class, SessionScoped.class).build();
 
+    @Inject
+    String producedString;
+
     @Test
     public void testNormalScopes() {
         Assertions.assertEquals(weld.select(Foo.class).get().getId(), weld.select(Foo.class).get().getId());
         Assertions.assertEquals(weld.select(Oof.class).get().getId(), weld.select(Oof.class).get().getId());
+        Assertions.assertNotNull(producedString);
+        Assertions.assertEquals("foo", producedString);
     }
 
 }
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/contexts/RequestScopedProducer.java b/junit5/src/test/java/org/jboss/weld/junit5/contexts/RequestScopedProducer.java
new file mode 100644
index 0000000..deae051
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/contexts/RequestScopedProducer.java
@@ -0,0 +1,15 @@
+package org.jboss.weld.junit5.contexts;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Produces;
+
+@RequestScoped
+public class RequestScopedProducer {
+
+    @Produces
+    @Dependent
+    public String produceDependentString() {
+        return "foo";
+    }
+}