7 Replies Latest reply on Apr 26, 2012 6:52 PM by igarashitm

    injecting InitialContext and ServiceContainer into Arquillian junit testcase

    igarashitm

      Hi,

       

      I'm looking for the way to inject InitialContext and AS7 ServiceContainer into Arquillian junit testcase. With following testcase, ServiceContainer was null and InitlaContext failed to lookup from AS7 instance. Could you let me know if you find my mistake?

       

      • testcase
      package org.switchyard.test.arquillian.hello;
      import javax.inject.Inject;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      
      
      import junit.framework.Assert;
      
      
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.arquillian.test.api.ArquillianResource;
      import org.jboss.as.connector.ConnectorServices;
      import org.jboss.jca.core.spi.rar.ResourceAdapterRepository;
      import org.jboss.msc.service.ServiceContainer;
      import org.junit.Before;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      
      
      @RunWith(Arquillian.class)
      public class HelloArquillianTest {
      
      
          @Inject
          ServiceContainer _serviceContainer;
      
          @ArquillianResource
          InitialContext _initialContext;
      
          @Before
          public void before() {
              System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
          }
      
          @Test
          public void testInjection() throws Exception {
              Assert.assertNotNull(_serviceContainer);
              Assert.assertTrue(_serviceContainer.getService(ConnectorServices.RA_REPOSITORY_SERVICE) instanceof ResourceAdapterRepository);
          }
      
          @Test
          public void testArquillianResourceInjection() throws Exception {
              Assert.assertNotNull(_initialContext);
              Assert.assertTrue(_initialContext.lookup("java:/ConnectionFactory") instanceof javax.jms.ConnectionFactory);
          }
      }
      
      

       

      Other files including pom.xml and arquillian.xml are in the attached .jar file.

       

      Thanks,

      Tomo

        • 1. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
          igarashitm

          found one of those - Arquillian test should have at least one @Deployment - so added following.

           

              @Deployment
              public static JavaArchive createDeployment() {
                  return ShrinkWrap.create(JavaArchive.class)
                          .addClass(Hello.class)
                          .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
              }
          

           

           

          But hit an another problem

          Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [ServiceContainer] with qualifiers [@Default] at injection point [[field] @Inject private org.switchyard.test.arquillian.hello.HelloArquillianTest._serviceContainer]

                    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)

                    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)

                    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107)

                    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127)

                    at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346)

                    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331)

                    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)

                    at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83)

                    at org.jboss.as.weld.services.WeldService.start(WeldService.java:76)

                    ... 5 more

          • 2. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
            igarashitm

            Injection succeeded once I removed the beans.xml from @Deployment and changed the field accessibility to public, Thanks @aslak !!

            hmm, but should it be public anyway when I need to perform injection ???

             

            And then I'm seeing next error ... It seems that is class accessibility problem in the AS7. Some testcases in the AS7 testsuite are doing same thing(ex. PureTestCase in the testsuite/integration/smoke), so should be able to do though... maybe I need to ask it to the AS7 community.

            Caused by: java.lang.ClassNotFoundException: org.jboss.as.connector.ConnectorServices from [Module "deployment.49386822-451a-4cd6-89d4-466069ebf570.jar:main" from Service Module Loader]

                      at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190) [jboss-modules.jar:1.1.1.GA]

                      at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468) [jboss-modules.jar:1.1.1.GA]

                      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456) [jboss-modules.jar:1.1.1.GA]

                      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:423) [jboss-modules.jar:1.1.1.GA]

                      at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398) [jboss-modules.jar:1.1.1.GA]

                      at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120) [jboss-modules.jar:1.1.1.GA]

            • 3. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
              aslak

              private or public field shouldn't matter. It's up to the Enricher impl, but I believe most support privat fields as well as public.

               

              Does the PureTestCase define something extra in their MANIFEST.MF file?

              • 4. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
                igarashitm

                Hey, do we need field.setAccessible(true) right before field.set()?

                https://github.com/jbossas/jboss-as/blob/master/arquillian/testenricher-msc/src/main/java/org/jboss/arquillian/testenricher/msc/MSCTestEnricher.java

                 

                I couldn't find MANIFEST.MF for PureTestCase. I have noticed there is no module directory in the jbossHome for as7 testsuite/integration/smoke, so I may need some trick to achieve this, and it may not be applicable for our SwitchYard testkit ...

                • 5. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
                  aslak

                  hmm, yea that enricher doesn't allow for private fields. silly. report a issue

                  • 6. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
                    aslak

                    the modules dir is located some where else. I believe you can find the path defined in the testsuite/pom

                    • 7. Re: injecting InitialContext and ServiceContainer into Arquillian junit testcase
                      igarashitm