2 Replies Latest reply on Jul 31, 2012 10:07 PM by iapazmino

    Is Arquillian/ShrinkWrap so difficult to use?

    deadlock_gr

      Hi,

       

      This is not an attempt to start a flame, I really have very high expectations of Arquillian.

       

      However, I think that either the documentation is misleading on how easy and straightforward it is to write a test for a real application, or I have a completely wrong picture and I am using Arquillian the wrong way.

       

      I am trying to test EJBs of a multi-module Maven application on Weblogic. The EJB module, which is the one containing the EJBs I want to test, ends up having dozens of dependencies.

       

      So, manually creating the archive to be deployed with SkrinkWrap (either class-by-class or package-by-package) is simply too complicated.

       

      I also found the MavenDependencyResolver which gave me hopes, but this also is not without problems. First of all, it was in Alpha or Beta stage some weeks ago (may still be), secondly is not documented in the arquillian guides, and most importantly. Most importantly, although you can actually use it to retrieve all the dependencies of an EJB-jar, you cannot use it to build up an EAR, as if you place these dependencies in the EAR/lib directory, you will also include EJB-jar dependencies,which should instad go at the top level of the EAR.

       

      Bottom line: Unless I am overseeing something here, it has been hellishly difficult to create an EJB-jar with all its dependencies in place. I would even be OK using the top-level EAR project that my Maven build already prepares, but from the documentation I can see that this is not the by-the-book way.

       

      Is Arquillian/ShrinkWrap so difficult to use?

        • 1. Re: Is Arquillian/ShrinkWrap so difficult to use?
          plexusnexus


          Hi Markos,

           

          you are right, building an EAR with many dependencies is not the easiest thing to do, but possible if you use Maven and follow the following steps:

           

          1. Create a separete POM with all the dependencies you want do include in your EAR deployment. Call it test-dependencies.xml and put it below src/test/resources. It should inherit your parent POM to reuse all the managed dependencies.
          2. Ensure a unique and meaning foll naming schema for your Maven artifact. The artifactId should allow you to determine the type of the artifact (EAR, WAR or simple Jar). All my artifacts containing EJBs have the suffix service.
          3. Configure your MavenDepencyResolver with the test-dependencies.xml POM like this:

             

            String pom = RegistrationServiceArquillianModuleTest.class.getResource("/test-dependencies.xml").getFile();

                   EnterpriseArchive earArchive = ShrinkWrap.create(EnterpriseArchive.class, "test.ear");

                   MavenDependencyResolver resolver = DependencyResolvers.use(MavenDependencyResolver.class)

                            .includeDependenciesFromPom(pom)

                            .goOffline();

          4. Then resolve the artifacts to add as module and as libs separately using MavenResolutionFilter:
            Collection ejbArchives = resolver.resolveAs(JavaArchive.class, new MavenResolutionFilter() {
                        private final Pattern COMPILE = Pattern.compile(":");
            
                        @Override
                        public boolean accept(MavenDependency mavenDependency) {
                            String[] coords = COMPILE.split(mavenDependency.getCoordinates());
            
                            boolean isService = coords[1].endsWith("service");
                            boolean isOurs = coords[0].startsWith("my.company");
                            boolean toBeIncluded = isOurs && isService;
            
                            if (toBeIncluded) {
                                System.out.println("=> " + mavenDependency.getCoordinates());
                            }
            
                            return toBeIncluded;
                        }
            
                        @Override
                        public MavenResolutionFilter configure(Collection mavenDependencies) {
                            return null;
                        }
                    });
            
            
                    Collection libArchives = resolver.resolveAs(JavaArchive.class, new MavenResolutionFilter() {
                        private final Pattern COMPILE = Pattern.compile(":");
            
                        @Override
                        public boolean accept(MavenDependency mavenDependency) {
                            String[] coords = COMPILE.split(mavenDependency.getCoordinates());
            
                            boolean isService = coords[1].endsWith("service");
                            boolean isOurs = coords[0].startsWith("my.company");
            
                            boolean isLibrary = !(isOurs && (isService));
            
                            return isLibrary;
                        }
            
                        @Override
                        public MavenResolutionFilter configure(Collection mavenDependencies) {
                            return null;
                        }
                    });
            
            
                    JavaArchive jarModule = ShrinkWrap.create(JavaArchive.class, createJarName())
                            .addPackage(RegistrationService.class.getPackage())
                            .addClass(RegistrationServiceTestHelperBean.class)
                            .addAsManifestResource("META-INF/persistence.xml", "persistence.xml");
            
                    WebArchive warArchive = ShrinkWrap.create(WebArchive.class, createWarName())
                            .addClass(RegistrationServiceArquillianModuleTest.class);
            
            
                    // Now add the tests!
                    earArchive.addAsManifestResource("glassfish-application.xml")
                            .addAsManifestResource("glassfish-ejb-jar.xml")
                            .addAsModules(warArchive)
                            .addAsModule(jarModule)
                            .addAsLibraries(libArchives);
            
                    for (JavaArchive javaArchive : ejbArchives) {
                        earArchive.addAsModule(javaArchive);
                    }
            
                    return earArchive;
          5. That is all.

           

          This is the way it works for me.

           

          Bye,

           

          Oliver

          • 2. Re: Is Arquillian/ShrinkWrap so difficult to use?
            iapazmino

            You  might find this guide useful http://arquillian.org/guides/shrinkwrap_introduction