4 Replies Latest reply on Feb 19, 2015 12:53 PM by Victor Vidigal Ribeiro

    Arquillian Inject external project class

    Victor Vidigal Ribeiro Newbie

      Hello, I'm trying run tests with Arquillian.

      I created a new project with my tests and I'm importing a War file with the application under test. I can deploy the war with the method showed bellow:

       

      @Deployment(name = "jabot")

      public static WebArchive jabotDeployment() {

           final WebArchive herbarioVirtualWar = ShrinkWrap .create(ZipImporter.class, "herbariovirtual.war") .importFrom(new File("lib/jabot-herbarioVirtual-1.0.jar")) .importFrom(new File("lib/jabot-herbarioVirtual-web-1.0.war")) .as(WebArchive.class);      herbarioVirtualWar.addClass(JBossASIntegrationWarTest.class);

           LOGGER.info(herbarioVirtualWar.toString(Formatters.VERBOSE));

           return herbarioVirtualWar;

      }

       

      The problem is when I try inject a class from my war project:

      @EJB private HerbarioVirtualHandlerBeanImpl hb;

       

      When I insert this code I get an error:

      -------------------------------------------------------------------------------

      Test set: org.example.arquillian.JBossASIntegrationEarTest

      -------------------------------------------------------------------------------

      Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.015 sec <<< FAILURE!

      initializationError(org.example.arquillian.JBossASIntegrationEarTest)  Time elapsed: 0.014 sec  <<< ERROR!

      java.lang.NoClassDefFoundError: Lbr/gov/jbrj/jabot/cs/colecoes/herbarioVirtual/HerbarioVirtualHandlerBeanImpl;

          at java.lang.Class.getDeclaredFields0(Native Method)

          at java.lang.Class.privateGetDeclaredFields(Class.java:2499)

          at java.lang.Class.getDeclaredFields(Class.java:1811)

          at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)

          at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)

          at org.junit.runners.model.TestClass.<init>(TestClass.java:57)

          at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)

          at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)

          at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)

          at org.jboss.arquillian.junit.Arquillian.<init>(Arquillian.java:58)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

          at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

          at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

          at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)

          at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)

          at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)

          at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)

          at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)

          at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)

          at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:51)

          at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)

          at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:606)

          at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)

          at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)

          at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)

          at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)

          at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

      Caused by: java.lang.ClassNotFoundException: br.gov.jbrj.jabot.cs.colecoes.herbarioVirtual.HerbarioVirtualHandlerBeanImpl

          at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

          at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

          at java.security.AccessController.doPrivileged(Native Method)

          at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

          at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

          at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

          at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

          ... 32 more

       

       

      Could someone  help me?

       

       

      ** There is a detail. The class HerbarioVirtualHandlerBeanImpl is into lib/jabot-herbarioVirtual-1.0.jar

        • 1. Re: Arquillian Inject external project class
          Aslak Knutsen Master

          The structure of a WAR and JAR are different, so just importing one into the other makes no sense.

           

          Try doing a System.out.println(archive.toString(true)) in the deployment method to see how the generated structure is.

           

          Possible what you want is war.merge('WEB-INF/classes', importJarArchive)

          1 of 1 people found this helpful
          • 2. Re: Arquillian Inject external project class
            Victor Vidigal Ribeiro Newbie

            I'm trying a lot of ways to test my application. I think that you can help me if I'm in the right path.

            I have an application and I can't include Arquillian in this application (it's a maven1 application). So, I generate a war (I try EAR too) and import this war into my eclipse test project.

            The first question is: Is it possible?

            • 3. Re: Arquillian Inject external project class
              Aslak Knutsen Master

              In general what you're doing is correct. But, the ZpiImporter doesn't know anything about the structure of a JavaArchive or a WebArchive. Meaning it doesn't know that in a WebArchive a Class file should not be on / (root), but in WEB-INF/classes. So when you just merge a JavaArchive into a WebArchive you get a structure not recognized by the application server. This is probably the cause of your class not found exception.

               

              Try something like

               

                      JavaArchive jar = ShrinkWrap.createFromZipFile(JavaArchive.class, new File("lib/jabot-herbarioVirtual-1.0.jar"));

                     

                      WebArchive war = ShrinkWrap.createFromZipFile(WebArchive.class, new File("lib/jabot-herbarioVirtual-web-1.0.war"))

                      war.merge(jar, "WEB-INF/classes");

              1 of 1 people found this helpful
              • 4. Re: Arquillian Inject external project class
                Victor Vidigal Ribeiro Newbie

                I think it will work. But, after fix some erros like "&" caracter in web.xml, I got new error.

                The new error is

                java.lang.IllegalArgumentException: ArquillianServletRunner not found. Could not determine ContextRoot from ProtocolMetadata, please contact DeployableContainer developer. at org.jboss.arquillian.protocol.servlet.ServletUtil.determineBaseURI(ServletUtil.java:64) at org.jboss.arquillian.protocol.servlet.ServletURIHandler.locateTestServlet(ServletURIHandler.java:60) at org.jboss.arquillian.protocol.servlet.ServletMethodExecutor.invoke(ServletMethodExecutor.java:84)

                 

                My deployment method is

                @Deployment(name = "jabot")

                public static WebArchive jabotDeployment(){

                JavaArchive jabotJar = ShrinkWrap.createFromZipFile(JavaArchive.class, new File("lib/jabot-herbarioVirtual-1.0.jar"));

                WebArchive jabotWar = ShrinkWrap.createFromZipFile(WebArchive.class, new File("lib/jabot-herbarioVirtual-web-1.0.war"));

                jabotWar.merge(jabotJar);

                LOGGER.info(jabotWar.toString(Formatters.VERBOSE));

                return jabotWar;

                }


                I saw that there is a context-root declaration inside jabot-herbarioVirtual-web-1.0.war/WEB-INF/jboss-web.xml

                <jboss-web>

                <context-root>/jabot/herbarioVirtual</context-root>

                </jboss-web>


                Could you help me again?

                Thankyou for your help!