title: Inner dependency is not being injected (Weld + JavaSE)
body: <p>I am in the process of refactoring an old module, by adding CDI.</p>
  
  <p>I end with</p>
  
  <pre><code>public interface ApiFactory {
    ...
  }
  
  public class ApiFactorySp
      implements ApiFactory {
    @Inject
    UrlProducer urlProducer;  // &lt;-- Does not get injected
    ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public interface UrlProducer {
      public String getUrl();
  }
  
  @Alternative
  public class UrlProducerTest
      implements UrlProducer {
  
      @Override
      public String getUrl() {
          return "https://myTestEnv.mydomain/myWebApp";
      }
   }
  </code></pre>
  
  <p>For testing, I create a <code>beans.xml</code> file in <code>META-INF</code>: </p>
  
  <pre><code>&lt;beans
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all"&gt;
      &lt;alternatives&gt;
        &lt;class&gt;myOrg.myProject.myPackage.UrlProducerTest&lt;/class&gt;
      &lt;/alternatives&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>To test it, I am doing like shown <a href="http://blog.rocketscience.io/dependency-injection-with-cdi-in-java-se/" rel="nofollow">in this blog</a></p>
  
  <pre><code>public class WeldContext {
  
      public static final WeldContext INSTANCE = new WeldContext();
  
      private final Weld weld;
      private final WeldContainer container;
  
      private WeldContext() {
          this.weld = new Weld();
          this.container = weld.initialize();
          Runtime.getRuntime().addShutdownHook(new Thread() {
  
              @Override
              public void run() {
                  weld.shutdown();
              }
          });
      }
  
      public &lt;T&gt; T getBean(Class&lt;T&gt; type) {
          return container.instance().select(type).get();
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
  
      public WeldJUnit4Runner(Class&lt;Object&gt; clazz) throws InitializationError {
          super(clazz);
      }
  
      @Override
      protected Object createTest() {
          final Class&lt;?&gt; test = getTestClass().getJavaClass();
          return WeldContext.INSTANCE.getBean(test);
      }
  }
  </code></pre>
  
  <p>Now, when I try to test the logic, I do </p>
  
  <pre><code>@RunWith(WeldJUnit4Runner.class)
  public class MyTest {
       @Inject
       UrlProducer urlProducer;         
  
       @Inject
       ApiFactory apiFactory;
  
       @Test
       public void test() {
           apiFactory.doSomethingThatRequiresUrlProducer();
       }
   }
  </code></pre>
  
  <p>When I run this, both of the test attributes are inject, but I get NPE because the <code>urlProducer</code> attribute inside of the <code>apiFactory</code> instance has not been assigned a value.</p>
  
  <p>Why is Weld not recognizing the <code>@Inject</code> attribute inside <code>ApiFactory</code>?</p>
  
  <p>JDK 7, Weld 2.2.10, Junit 4.12</p>
  
  <p>UPDATE: After posting the question, started trying with a simpler, brand new project (with just two interfaces and three classes). Using Weld "standalone" did not solve the issue, using <a href="http://jglue.org/cdi-unit/" rel="nofollow">CDI-Unit</a> did solve it.</p>
  
  <p>Then I modified my original project to use CDI-Unit, but it did not improve anything. After that I change the injection of <code>UrlProducerTest</code> in <code>ApiFactory</code> from field to constructor (i.e., defining the <code>@Inject ApiFactory(UrlProducer urlProducer)</code> constructor) solved it. I still have not tried this solution with "standalone" Weld (that is for tomorrow), but nonetheless I am still interested in know why field injection is not working.</p>