5 Replies Latest reply on Apr 26, 2013 8:47 AM by developer74

    Problems with @Inject

    developer74

      Hi altogether,

       

      I just started with Arquillian, which runs basically fine. But now I faced a problem with the following situation, where the @Inject is not filled:

       

      I have a basic JavaEE6 webproject (Maven), and now there is (every class is in its own file):

       

      @Named
      class A {
          @Inject
          private B b;
      
          public void execute() {
              System.out.println(b.toString());
          }
      
      }
      
      
      @Named
      class B {
      
          public String toString() {
              return "Here is B";
          }
      }
      
      
      
      In test class:
      
          @Deployment
          public static WebArchive create() {
              return ShrinkWrap.create(WebArchive.class).
                      addClasses(A.class, B.class).
                      addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
          }
      
      

       

      In the JUnit4-Testcase, I have the "@RunWith" and the "@Deployment" annotation, and I have class A and B "added", and when I @Inject class B IN the testcase, it is injected and worked. But if I have @Inject for class A in the tast case, class A is correctly injected, but when I call the execute() method, I get a NullPointerException, because B is not injected in class A.

      I have also the beans.xml in the META-INF under "src/test/resources".

       

      Because the injection basically works, but not in the dependent class, I think there is a little bit, which I have missed.

       

      I would be very glad if someone directs me in the right way ;-), thanks in advance,

      Ulrich

        • 1. Re: Problems with @Inject
          bmajsak

          Hi Urlich,

           

          can you give us a bit more details about your setup? Arquillian-relevant parts of pom, configuration of arquillian itself and your target container would be a good start to nail down the problem.

          • 2. Re: Problems with @Inject
            developer74

            Hi Bartosz,

             

            Thanks for your reply, I try to provide the required informations.

            The WebApp should run on a Glassfish server (newst version 3.1.2)

             

            The interesting part of the pom.xml is:

             

             

                <dependencyManagement>
                    <dependencies>
                        <dependency>
                            <groupId>org.jboss.arquillian</groupId>
                            <artifactId>arquillian-bom</artifactId>
                            <version>1.0.3.Final</version>
                            <scope>import</scope>
                            <type>pom</type>
                        </dependency>
                    </dependencies>
                </dependencyManagement>
            
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.main.extras</groupId>
                        <artifactId>glassfish-embedded-all</artifactId>
                        <version>3.1.2.2</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>rhino</groupId>
                        <artifactId>js</artifactId>
                        <version>1.7R2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.commons</groupId>
                        <artifactId>commons-lang3</artifactId>
                        <version>3.1</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>2.4</version>
                    </dependency>
            
                    <!-- Testing Dependencies -->
                    <!-- Arquillian -->
                    <dependency>
                        <groupId>org.jboss.arquillian.junit</groupId>
                        <artifactId>arquillian-junit-container</artifactId>
                        <scope>test</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.jboss.arquillian.container</groupId>
                        <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                        <version>1.0.0.CR3</version>
                        <scope>test</scope>
                    </dependency>
                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.11</version>
                        <scope>test</scope>
                    </dependency>

             

             

            Und der Testcase sieht dann so aus:

             

            @RunWith(Arquillian.class)
            public class InjectTest {
            
                @Inject
                A a;
            
                @Deployment
                public static WebArchive create() {
                    return ShrinkWrap.create(WebArchive.class).
                            addClasses(A.class, B.class).
                            addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
                }
            
                @Test
                public void testExecute() throws Exception {
                    a.execute();
                }
            }
            
            

            • 3. Re: Problems with @Inject
              developer74

              I think, I found out, what was wrong. For any reason, I had to add the Rhino-library to the @Deployment method. I couldn't explain why I need especially this library and not, or example, slf4j.

               

               

              In the pom.xml, this dependency has to be added:

               

              <dependency>
                          <groupId>org.jboss.shrinkwrap.resolver</groupId>
                          <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
                          <scope>test</scope>
              </dependency>
              

               

              In the test-case, the @Deployment has to be extended for "addAsLibraries()":

               

              @Deployment
                  public static WebArchive create() {
                      MavenDependencyResolver resolver = DependencyResolvers.use(
                              MavenDependencyResolver.class).loadMetadataFromPom("pom.xml");
                      return ShrinkWrap.create(WebArchive.class).
                              addClasses(A.class, B.class).
                              addAsLibraries(resolver.artifact("rhino:js:1.7R2").
                                      resolveAsFiles()).
                              addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
                  }
              

               

               

              Is there some rule, which libraries have to be added to the "test-webarchive" and which not?

              • 4. Re: Problems with @Inject
                aslak

                The general rule would be the ones you use that are not provided by the container.

                • 5. Re: Problems with @Inject
                  developer74

                  Ah, ok, that explains why default logging-frameworks need not to be added (they are for sure provided by the container)...

                  Thanks, Arqulllian is real helpful and now it works perfectly for me...