7 Replies Latest reply on Dec 9, 2015 7:15 AM by mjobanek

    Arquillian Glassfish - Managed or Embedded?

    hpropes

      Hi,

       

      In production, I'm writing Java EE Web Applications using Maven 2, Glassfish 4.1, and Netbeans 8. For testing, I'd like to use Arquillian with a Glassfish environment (either embedded or managed, I need guidance).  Yesterday I followed the tutorial at Getting Started · Arquillian Guides, adapting it to my own setup.

       

      I did not use the maven archetype or JForge.  I simply attempted to add the Arquillian dependencies to my own project's pom.xml.

       

      Here are the Arquillian-related dependencies in my pom.xml:

       

      <dependency>

                  <groupId>org.jboss.spec</groupId>

                  <artifactId>jboss-javaee-7.0</artifactId>

                  <version>1.0.3.Final</version>

                  <type>pom</type>

                  <scope>provided</scope>

              </dependency>

              <dependency>

                  <groupId>org.jboss.arquillian</groupId>

                  <artifactId>arquillian-bom</artifactId>

                  <version>1.1.10.Final</version>

                  <scope>provided</scope>

                  <type>pom</type>

              </dependency>

              <dependency>

                  <groupId>org.jboss.arquillian.junit</groupId>

                  <artifactId>arquillian-junit-container</artifactId>

                  <version>1.1.10.Final</version>

                  <scope>test</scope>               

              </dependency>

              <dependency>

                  <groupId>org.jboss.shrinkwrap</groupId>

                  <artifactId>shrinkwrap-api</artifactId>

                  <version>1.2.2</version>

                  <type>jar</type>

              </dependency>   

      <profiles>

              <profile>

                  <id>arquillian-glassfish-embedded</id>

                  <dependencies>

                      <dependency>

                          <groupId>org.jboss.arquillian.container</groupId>

                          <artifactId>arquillian-glassfish-embedded-3.1</artifactId>

                          <version>1.0.0.CR4</version>

                          <scope>test</scope>

                      </dependency>               

                  </dependencies>

              </profile>

          </profiles>

       

       

       

      These dependencies are from a parent pom.xml that I am required to use:

       

      <dependency>

                  <groupId>junit</groupId>

                  <artifactId>junit</artifactId>

                  <version>4.12</version>

                  <scope>test</scope>

                  <optional>true</optional>

              </dependency>

              <dependency>

                  <groupId>org.glassfish.main.extras</groupId>

                  <artifactId>glassfish-embedded-all</artifactId>

                  <version>4.1</version>

                  <scope>test</scope>

                  <exclusions>

                      <exclusion>

                          <groupId>com.sun</groupId>

                          <artifactId>tools-jar</artifactId>

                      </exclusion>

                  </exclusions>

              </dependency>

       

      <dependency>

                  <groupId>javax</groupId>

                  <artifactId>javaee-api</artifactId>

                  <version>7.0</version>

                  <scope>provided</scope>

                  <exclusions>

                      <exclusion>

                          <artifactId>activation</artifactId>

                          <groupId>javax.activation</groupId>

                      </exclusion>

                      <exclusion>

                          <artifactId>javax.mail</artifactId>

                          <groupId>com.sun.mail</groupId>

                      </exclusion>

                  </exclusions>

              </dependency>

       

      <dependency>

                  <groupId>org.slf4j</groupId>

                  <artifactId>slf4j-api</artifactId>

                  <version>1.7.12</version>

                  <optional>${skinny.war}</optional>

              </dependency>

              <dependency>

                  <groupId>org.slf4j</groupId>

                  <artifactId>slf4j-jdk14</artifactId>

                  <version>1.7.12</version>

                  <optional>${skinny.war}</optional>

              </dependency>

       

      <plugins>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-compiler-plugin</artifactId>

                      <version>3.3</version>

                      <configuration>

                          <source>${java.code.version}</source>

                          <target>${java.code.version}</target>

                      </configuration>

                  </plugin>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-surefire-plugin</artifactId>

                      <version>2.18.1</version>

                      <configuration>

                          <includes>

                              <include>**/test/**</include>                    

                          </includes>

                      </configuration>

                  </plugin>

              </plugins>

       

       

       

      I wrote a test file that looks like this:

       

      /**

      *

      * @author hpropes

      */

      public class SampleTest

      {

       

           @Deployment

          public static JavaArchive createDeployment() {

              JavaArchive jar = ShrinkWrap.create(JavaArchive.class)

                      .addClass(SampleBean.class)

                      .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")

                      .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

              System.out.println(jar.toString(true));

              return jar;

          }

         

          @Inject private SampleBean sample;

         

          @Test

          public void showSample(){

              Assert.assertEquals(5, sample.getMyValue());

              System.out.println(String.format("sample myValue is %d", sample.getMyValue()));

          }

      }

       

      Which tests this bean:

       

      @Model

      public class SampleBean

      {

          private @Getter final int myValue = 5;

      }

         

       

      I run this test and I get a NullPointerException on this line of my test class:

       

      Assert.assertEquals(5, sample.getMyValue());

       

      Apparently CDI in my test class is not working.

       

      Can anyone help with this? I've heard rumors that I can also use arquillian-glassfish-managed instead of arqueillian-glassfish-embedded. What is the difference between the two? Is there a complete tutorial for setting up Arquillian in a modern Glassfish/Java EE/Netbeans/Maven environment?:

       

      Any suggestions would be GREAT.

       

      thanks, Heather

        • 1. Re: Arquillian Glassfish - Managed or Embedded?
          mjobanek

          Hi,

          regarding to your question about the container types (remote/managed/embeded) Containers - Arquillian - Project Documentation Editor

           

          regarding to your problem:

          1) I would guess that the the beans.xml is a WEB-INF resources, so you need to add it correctly: .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

          2) Are there any errors or warnings in the server log?

           

          Small recommendation:

          the BOM artifacts (like arquillian-bom) should be added into the "dependencyManagement" part http://stackoverflow.com/questions/14874966/how-to-use-bom-file-with-maven

           

          Glassfish testing:

          you can find some useful information here Arquillian Tests on glassfish 4.1

          • 2. Re: Arquillian Glassfish - Managed or Embedded?
            hpropes

            Thank you for your reply Matous. Sorry I did not reply sooner but have been caught up with other projects and the holiday.

             

            I have tried using a <dependencyManagement> block in my parent pom.xml for arguillian-bom like this:

             

            <dependencyManagement>

                    <dependencies>

                        <dependency>

                            <groupId>org.jboss.arquillian</groupId>

                            <artifactId>arquillian-bom</artifactId>

                            <version>1.1.10.Final</version>

                            <scope>provided</scope>

                            <type>pom</type>

                        </dependency>          

                    </dependencies>

                </dependencyManagement>

             

            and then including the arquillian-junit dependencies in my child pom.xml without a version, like this:

             

            <dependency>

                        <groupId>org.jboss.arquillian.junit</groupId>

                        <artifactId>arquillian-junit-container</artifactId>        

                        <scope>test</scope>

                    </dependency>

             

            But netbeans just gives me this error, even if I purge the .m2 repo:

             

            [ERROR] 'dependencies.dependency.version' missing

             

            Starting with the configuration in my original post at top, I tried adding this line to my test class:

             

            @RunWith(Arquillian.class)

            public class SampleTest

             

            but then I get this error:

             

            java.lang.RuntimeException: Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:165)

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)

                at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)

                at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:113)

                at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)

                at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)

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

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

                at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)

                at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)

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

            Caused by: java.lang.reflect.InvocationTargetException: null

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

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

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

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

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:161)

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)

                at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)

                at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:113)

                at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)

                at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)

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

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

                at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)

                at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)

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

            Caused by: java.lang.NoSuchMethodError: org.jboss.shrinkwrap.descriptor.api.DescriptorImporter.fromStream(Ljava/io/InputStream;)Lorg/jboss/shrinkwrap/descriptor/api/Descriptor;

                at org.jboss.arquillian.config.impl.extension.ConfigurationRegistrar.resolveDescriptor(ConfigurationRegistrar.java:83)

                at org.jboss.arquillian.config.impl.extension.ConfigurationRegistrar.loadConfiguration(ConfigurationRegistrar.java:57)

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

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

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

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

                at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)

                at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)

                at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)

                at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)

                at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)

                at org.jboss.arquillian.core.impl.ManagerImpl.start(ManagerImpl.java:290)

                at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.<init>(EventTestRunnerAdaptor.java:63)

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

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

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

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

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:161)

                at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:102)

                at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)

             

            ...

             

            Without "@RunWith(Arquillian.class)" I get this error, (the NPE I refer to in my original post):

             

            Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE! - in com.baw.website.test.arquillian.SampleTest

            showSample(com.baw.website.test.arquillian.SampleTest)  Time elapsed: 0.001 sec  <<< ERROR!

            java.lang.NullPointerException: null

                at com.baw.website.test.arquillian.SampleTest.showSample(SampleTest.java:46)

             

            line 46 of SampleTest is in red:

             

            @Inject private SampleBean sample;

             

                @Test

                public void showSample(){

            46>      Assert.assertEquals(5, sample.getMyValue());

                    System.out.println(String.format("sample myValue is %d", sample.getMyValue()));

                }

             

            Can I use the "Getting Started - Arquillian Guides"  as a starting point? If not is there another good tutorial on how to setup Arquillian with GF 4.1?  I looked at the JavaEE samples referred to in the "Arquillian Tests on Glassfish 4.1" thread, but, but did not see a pom.xml that referred to Glassfish 4.1. setup and there was no tutorial. Also I could not even get the project to build due to the "[ERROR] 'dependencies.dependency.version' missing" error that I mention above. Right now I'm just going in circles with dependencies.

             

            thanks,

             

            Heather Propes

            • 3. Re: Arquillian Glassfish - Managed or Embedded?
              hpropes

              ok, I am seeing that the javaee7 sample app at javaee-samples/javaee7-samples · GitHub

               

              has the profiles:

              glassfish-remote-arquillian and glassfish-embedded-arquillian

               

              which might work as a reference. I'm going to try to get them to run...

              • 4. Re: Arquillian Glassfish - Managed or Embedded?
                hpropes

                It is going better now. I'm having some success with the javaee 7 sample project at https://github.com/javaee-samples/javaee7-samples
                 
                  The configuration is much more organized and clear in the sample project. Here the GF choices seem to be:
                 
                  glassfish-embedded-arquillian (embedded, starts every time you run a test)
                 
                  glassfish remote-arquillian (gf starts once and tests connect to that)
                 
                  I got the embedded one to work and am trying for the remote. My only problem with the remote is configuring my admin url. I'm using a non-standard admin console port 49843 and I'm getting:
                 
                  org.jboss.arquillian.container.glassfish.CommonGlassFishManager start
                  SEVERE: Could not connect to DAS on:  http://localhost:4848 | Connection refused


                But I figured out the solution within the javaee7 samples app. In javaee7-samples/test-utils/src/main/resources/arquillian.xml I added:


                <container qualifier="glassfish">

                        <configuration>

                            <property name="adminPort">49834</property>

                        </configuration>

                    </container>


                And in the parent pom I added this to the end of the glassfish-remote-arquillian profile, to invoke the port I had added above with the "glassfish" qualifier:


                <build>

                                <plugins>

                                    <plugin>

                                        <artifactId>maven-surefire-plugin</artifactId>

                                        <configuration>

                                            <systemPropertyVariables>

                                                <arquillian.launch>glassfish</arquillian.launch>

                                            </systemPropertyVariables>                           

                                        </configuration>

                                    </plugin>

                                </plugins>

                </build>


                So far things are going well. I have gotten cdi/beansxml-noversion/src/test/java/org/javaee7/cdi/beansxml/noversion/GreetingTest.java to work in the javaee7-samples program using arquillian/junit/glassfish 4.1  and build/deploy in Netbeans.

                • 5. Re: Arquillian Glassfish - Managed or Embedded?
                  mjobanek

                  Hi,

                   

                  sorry for my late response.

                  It's good to know that you are in the right way.

                   

                  BTW. If you want to know what parameters are possible to use there in the Arquillian gf configuration, you can start finding here: arquillian-container-glassfish/CommonGlassFishConfiguration.java at master · arquillian/arquillian-container-glassfish ·…

                   

                  You have probably noticed that your former usage of the BOM:

                               <dependency>
                                  <groupId>org.jboss.arquillian</groupId>
                                  <artifactId>arquillian-bom</artifactId>
                                  <version>1.1.10.Final</version>
                                  <scope>provided</scope>
                                  <type>pom</type>
                              </dependency>
                  

                  was wrong - you used a wrong scope. In the case of BOM you have to use the "import" scope - see: Maven – Introduction to the Dependency Mechanism

                  • 6. Re: Arquillian Glassfish - Managed or Embedded?
                    hpropes

                    Hi Matous,

                     

                    Thanks for writing back and I'll change the scope of my BOM dependency. The Arquiillian code in the JavaEE examples worked fine, but it was simple and self-contained. Once I got back into my real project, which is complicated with parent-poms and a million external libraries and dependencies, I started having trouble again. Most of my troubles come from ShrinkWrap and JPA.

                     

                    My main goal right now is to test JPA code. I'd like my test just to call my DAO object. I don't want to include the EntityManager in my test and make PQL calls.

                     

                    In ShrinkWrap, do I need to explicitly declare every dependency needed for my test? Currently I have something like:

                     

                    5x addAsResource

                    2x addAsLibraries

                    6x addPackage

                    10x addClass

                     

                    I have gotten lots of different errors. Some of them are:

                     

                    "Exception while preparing the app : Unable to load the EJB module. DeploymentContext? does not contain any EJB. Check the archive to ensure correct packaging for"

                     

                    (I think this had to do with including beans.xml and web.xml incorrectly. I get it a lot but more recently I've had other errors).

                     

                    My most recent error has got to do with shiro:

                     

                    Exception while loading the app : java.lang.IllegalStateException?: ContainerBase?.addChild: start: org.apache.catalina.LifecycleException?: org.apache.shiro.config.ConfigurationException?: Unable to instantiate class [com.mypackage.StormpathRealm?] for object named 'stormpathRealm'. Please ensure you've specified the fully qualified class name correctly.. Please see server.log for more details. [status: CLIENT_ERROR reason: Bad Request]

                     

                    "stormpathRealm" is from shiro.ini which I included as an Arquillian resource. However Arquillian can't instantiate the class that stormpathRealm points to, although I've explicitly included that class in my ShrinkWrap as well.

                     

                    I'd love to get Arquillian and Shrinkwrap working but it is just so frustrating! Thanks.

                     

                    Heather

                    • 7. Re: Arquillian Glassfish - Managed or Embedded?
                      mjobanek

                      Hi,

                      In ShrinkWrap, do I need to explicitly declare every dependency needed for my test? Currently I have something like:

                      unfortunately yes - If you want to create the WAR deployment manually, you have to put everything necessary into the archive. There is a quickstart using JPA jboss-eap-quickstarts/tasks at 7.0.x-develop · jboss-developer/jboss-eap-quickstarts · GitHub Although it is intended for EAP, you can check there in the project's tests whether you are missing something.

                      Easier way to deploy an app is to use an already existing war file:

                      ShrinkWrap.createFromZipFile(WebArchive.class, "/path/to/deployment.war");
                      

                      however, I don't know if it is feasible in your case.

                       

                      My most recent error has got to do with shiro:

                       

                      Exception while loading the app : java.lang.IllegalStateException?: ContainerBase?.addChild: start: org.apache.catalina.LifecycleException?: org.apache.shiro.config.ConfigurationException?: Unable to instantiate class [com.mypackage.StormpathRealm?] for object named 'stormpathRealm'. Please ensure you've specified the fully qualified class name correctly.. Please see server.log for more details. [status: CLIENT_ERROR reason: Bad Request]


                      "stormpathRealm" is from shiro.ini which I included as an Arquillian resource. However Arquillian can't instantiate the class that stormpathRealm points to, although I've explicitly included that class in my ShrinkWrap as well.

                      If I understand it correctly - the problem is with deploying the archive, so this is not an exception from Arquillian but originated in the container - the container cannot instantiate the class. If so, then the war archive is not valid.

                      If the shiro is some 3rd party library - some maven dependency - I would recommend you to use ShrinkWrap Resolver to resolve the whole dependency with all transitive deps and then add everything as libraries into your archive. I did something similar recently: https://github.com/MatousJobanek/arquillian-test-crunch-workshop/blob/master/ui-ftests/src/test/java/org/jboss/arquillian/kitchensink/ui/ftests/KitchensinkTest.java#L38-L46

                      ShrinkWrap Resolver: shrinkwrap/resolver · GitHub

                       

                      Matous