1 2 Previous Next 17 Replies Latest reply: Apr 20, 2012 5:45 PM by Sueleyman Vurucu RSS

    Dependency Injection won't work

    Sueleyman Vurucu Expert

      Hi,

       

      I have an EAR-Archive with some modules ( JPA,EJB,WAR(JSF) and libs). So I want test my application with arquillian. But that seems not to be so easy.

       

      I think I don't understrand something.

       

      If I try to Inject a timerservice for example then my test fails.

       

       

      @RunWith(Arquillian.class)
      public class EditableServiceTest {
      
        @Deployment
        public static JavaArchive createTestArchive(){
          JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar");
          return archive;
        }
      
       @Resource
       TimerService tm;
      
        @Test
        public void test(){
          Assert.assertNotNull(tm);
        }
      
      }
      

       

      So I have no depency to my modules. And I'm wondered what I'm doing wrong.

       

      My arqullian.xml

       

       

      <?xml version="1.0" encoding="UTF-8"?>
      <arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://jboss.org/schema/arquillian"
          xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
          <!--         <protocol type="jmx-as7"> -->
          <container qualifier="jboss7" default="true">
              <protocol type="Servlet 3.0">
                  <property name="executionType">REMOTE</property>
              </protocol>
          <!--
          <configuration>
                  <property name="jbossHome">F:\IDE\jboss-as-7.1.1.Final</property>
                  <property name="javaVmArguments">-Xmx512m -XX:MaxPermSize=128m -Xrunjdwp:transport=dt_socket,address=8086,server=y,suspend=n</property>
              </configuration>
           -->
          </container>
      </arquillian>
      

       

      My pom

       

       

      ...
      <version-arquillian>1.0.0.CR7</version-arquillian>
       <version-jboss>7.1.1.Final</version-jboss>
      ...
      
      <dependencies>
              <dependency>
                  <groupId>org.apache.xalan</groupId>
                  <artifactId>serializer</artifactId>
                  <version>2.7.1-1.jbossorg</version>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.arquillian.protocol</groupId>
                  <artifactId>arquillian-protocol-servlet</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.arquillian.junit</groupId>
                  <artifactId>arquillian-junit-container</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.as</groupId>
                  <artifactId>jboss-as-arquillian-container-remote</artifactId>
                  <version>${version-jboss}</version>
              </dependency>
          </dependencies>
      

       

      My runtime,

       

      WinXP,

      JBOSS 7.1.1.Final

      Java 7u3

      Maven 3.04

       

      Thanks in advance

        • 1. Re: Dependency Injection won't work
          Sueleyman Vurucu Expert

          I forgot to say that I try the injection with @Resource and @EJB too.

          • 2. Re: Dependency Injection won't work
            Sueleyman Vurucu Expert

            I try the test with an EAR deployment. But still the same result.

             

             

            @RunWith(Arquillian.class)

            public class EditableServiceTest {

             

              @Deployment

              public static EnterpriseArchive createTestArchive(){

                return DependencyResolvers

                    .use(MavenDependencyResolver.class)

                    .artifact(

                        "groupID:artifactID:ear:3.0.0-SNAPSHOT")

                    .resolveAs(EnterpriseArchive.class).iterator().next()

                    .addAsLibrary(ShrinkWrap.create(JavaArchive.class).addClass(EditableServiceTest.class));

              }

             

              @EJB

              public DeviceOrderDAO edao;

             

              @Test

              public void test(){

                Assert.assertNotNull(edao);

              }

             

             

            }

             

            But still the same. My service is null. I'm so confused. What I'm doing wrong ??

            • 3. Re: Dependency Injection won't work
              Aslak Knutsen Master

              TimerService injection is only available to EJB's to my knowledge, your TestClass is not deployed as a EJB

               

              Try adding @EJB.mappedName="jndi-name-to-where-it-is-bound" to your EJB injection, and similar with the @Resource injections your trying to do

              • 4. Re: Dependency Injection won't work
                Sueleyman Vurucu Expert

                I tried the injection with mapped name as well. But the result is the same. I thought I can use my testcase like an EjB. Or must I do special think so that the DI works.

                 

                Thanks in advance

                • 5. Re: Dependency Injection won't work
                  Aslak Knutsen Master

                  is it being deployed ok? Can you see the jndi names in the console log?

                  • 6. Re: Dependency Injection won't work
                    Sueleyman Vurucu Expert

                    Hi Aslak,

                    yes I can see the jndi names on the console. But what I don't understand is how the DI should work. I add my testcase in the EAR with    .addAsLibrary(ShrinkWrap.create(JavaArchive.class).addClass(EditableServiceTest.class).  For the DI my Testcase should be an EJB or not ?

                    Or mus I add my Testcase in an other way ??

                     

                    I have a further question. If I have more then one testcase ( I think that's the normal usecase )  would then arquillian restart the server in managed mode or redploy the ear in remote mode for every testcase ??

                    • 7. Re: Dependency Injection won't work
                      Aslak Knutsen Master

                      currently it will redeploy for every testclass, but keep the server instance running.

                       

                      The TestClass should only be a TestClass. Arquillain TestEnrichers should handle the DI part.

                       

                      Could you provide a complete example of what you don't get to work?

                      • 8. Re: Dependency Injection won't work
                        Sueleyman Vurucu Expert

                        Aslak Knutsen schrieb:

                         

                        currently it will redeploy for every testclass, but keep the server instance running.

                        That's realy pity. Is that planed for the future ? For some cases I need this behavior. And a redeloyment needs a new initialisationphase for my application.

                         

                         

                         

                        Aslak Knutsen schrieb:

                         

                        The TestClass should only be a TestClass. Arquillain TestEnrichers should handle the DI part.

                        Then I don't know why the DI don't work .

                         

                        Aslak Knutsen schrieb:

                         

                        Aslak Knutsen schrieb:

                         

                        Could you provide a complete example of what you don't get to work?

                        That's a little bit difficult. I must send the complete code of the warehouse management system of my company. My chef won't be so happy about this situation. . I can try tomorrow wirte a little example.

                        • 9. Re: Dependency Injection won't work
                          Aslak Knutsen Master

                          >That's realy pity. Is that planed for the future ? For some cases I need this behavior. And a redeloyment needs a new initialisationphase for my application.

                           

                          Yes, this is definitly planned


                          • 10. Re: Dependency Injection won't work
                            Sueleyman Vurucu Expert

                            Is there a timescheduöe for this feature ?

                            • 11. Re: Dependency Injection won't work
                              Sueleyman Vurucu Expert

                              Hi Aslak,

                               

                              I do an upload o updloaded.to. Is a very simple project based on maven. I hope that helps. For me of course

                               

                              http://uploaded.to/file/7wtuw82x

                              • 12. Re: Dependency Injection won't work
                                Sueleyman Vurucu Expert

                                Hi Aslak,

                                 

                                if I do the lookup with

                                 

                                  @EJB(mappedName="java:global/arq-test/arq-test-ejb/GreetingBean!com.siemag.arq.GreetingBeanLocal")

                                  public GreetingBeanLocal bean;

                                 

                                as you said then it will work. But the problem is my Appname varies in each project. But the common part for each project must be testable in everyy "special" project. I hope that was clear .

                                 

                                I see in the examples of arquillian this:

                                 

                                @RunWith(Arquillian.class)

                                public class GreetingManagerTest {

                                    @Deployment

                                    public static JavaArchive createDeployment() {

                                       return ShrinkWrap.create(JavaArchive.class, "test.jar")

                                          .addClasses(GreetingManager.class, GreetingManagerBean.class);

                                    }

                                 

                                    @EJB

                                    private GreetingManager greetingManager;

                                 

                                    @Test

                                    public void shouldGreetUser() throws Exception {

                                       String name = "Earthlings";

                                       assertEquals("Hello, " + name, greetingManager.greet(name));

                                    }

                                }

                                 

                                Why this DI not work with my code ?? I thought I can inject everything what I wan't with arquillian like I do in an EJB, isn't it so ??

                                • 13. Re: Dependency Injection won't work
                                  Sueleyman Vurucu Expert

                                  Hi Aslak does it work with uploaded.to ?? I don't find an email adress on your profile.

                                  • 14. Re: Dependency Injection won't work
                                    Vineet Reynolds Apprentice

                                    You could use the portable JNDI names of type "java:app/..." or "java:module/..." instead of "java:global/...". With the former two JNDI namespaces, you can perform lookups of resources that are local to the app, and thereby avoid the problem of adding the app-name to the JNDI name being looked up. You'll need to use java:app if you are performing a lookup of an EJB in a different module (the EJB is a different EJB module, compared to the Web module containing the Arquillian test). You can use java:module if the EJB and the Arquillian test are in the same module.

                                    1 2 Previous Next