3 Replies Latest reply on Nov 22, 2011 8:25 AM by aslak

    jacoco.exec file is almost empty

    lenn.angel

      Hi !

       

      I'm currently developping a library that will be used by multiple EJB. So I need to test this library, we use basic Junit test for some functions but for others we need that our library is embedded in an Java EE container so we used Arquillian with jacoco.

       

      So the Arquillian test do the following :

      - deploy a basic test ejb + the library

      - call a service of the ejb that use the library

       

      Everything works fine but the generated "jacoco.exec" is almost empty. It only contains jacoco headers. If I use it with sonar it plugin 0% coverage is done.

       

      Here my project architecture (I create a basic project to resolve this problem) :

       

      !http://www.monsterup.com/upload/1321955504980.png!

       

      My pom.xml :

      {code:xml}

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

         

          <modelVersion>4.0.0</modelVersion>

             

          <groupId>mygroupid</groupId>

          <artifactId>arquillianTest</artifactId>

          <version>1.0-SNAPSHOT</version>

          <packaging>jar</packaging>

          <name>ArquillianTest</name>

          <description></description>

       

           <dependencies>

              <dependency>

                  <groupId>junit</groupId>

                  <artifactId>junit</artifactId>

                  <version>4.8.2</version>

                  <type>jar</type>

                  <scope>test</scope>

              </dependency>

              <dependency>

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

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

                  <version>1.0.0.CR4</version>

                  <scope>test</scope>

              </dependency>

              <dependency>

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

                  <artifactId>arquillian-jbossas-managed-6</artifactId>

                  <version>1.0.0.CR2</version>

              </dependency>

              <dependency>

                  <groupId>org.jboss.jbossas</groupId>

                  <artifactId>jboss-server-manager</artifactId>

                  <version>1.0.3.GA</version>

              </dependency>

              <dependency>

                  <groupId>org.jboss.jbossas</groupId>

                  <artifactId>jboss-as-client</artifactId>

                  <version>6.0.0.Final</version>

                  <type>pom</type>

              </dependency>

              <dependency>

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

                  <artifactId>arquillian-protocol-servlet</artifactId>

                  <version>1.0.0.CR4</version>

              </dependency>

              <dependency>

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

                  <artifactId>arquillian-jacoco</artifactId>

                  <version>1.0.0.Alpha1</version>

              </dependency>

              <dependency>

                  <groupId>org.jacoco</groupId>

                  <artifactId>org.jacoco.core</artifactId>

                  <version>0.5.4.201111111111</version>

              </dependency>

          </dependencies>

      </project>

       

      {code}

       

      The Arquillian test code :

       

      {code}

      @RunWith(Arquillian.class)

      public class GreetingTest {

       

          @EJB

          GreetingManager manager ;

         

          @Deployment

          public static Archive createTestArchive() throws IllegalArgumentException, FileNotFoundException, IOException {

            

              // the archive for the EJB

              Package greetingPackage = GreetingTest.class.getPackage();

              JavaArchive greetingArchive = ShrinkWrap.create(JavaArchive.class, "greetingManager.jar").addPackages(true, greetingPackage);

             

              // the archive for my library that we want to test

              JavaArchive frameworkLib = ShrinkWrap.create(JavaArchive.class, "framework.jar").addPackages(true, "com/cs/framework");

       

               // Create an ear with the EJB and the library

              EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class);

              ear.addAsModule(greetingArchive);

              ear.addAsLibraries(frameworkLib);

             

              ear.as(ZipExporter.class).exportTo(new File("target/out.ear"), true);

       

       

              return ear;

          }

        

          @Test

          public void basicTest() throws Exception {

              String userName = "Earthlings";

              Assert.assertNotNull(manager);

              Assert.assertEquals("Hello " + userName, manager.greet(userName));

          }

      }

      {code}

       

      The EJB implementation :

      {code}

      public String greet(String userName)

         {

            MyClassToCover ctc = new MyClassToCover();

            ctc.setValue(userName.length()) ;

            

            return "Hello " + userName;

         }

      {code}

       

      After running here the content of the jacoco.exec :

      !http://www.monsterup.com/upload/1321955714327.png!

       

       

      I tried all of this versions :

      jacoco : 0.5.4.... , 0.5.3....

      jboss : 5.1 & 6.0 (both in managed and remote)

      arquillian-junit-container : CR2 - CR4

       

      Some ideas ?

       

      Thanks for the help !

       

      Full project in joined.

        • 1. Re: jacoco.exec file is almost empty
          aslak

          This could be related to how our jacoco extension store it's coverage data. I suspect the classloading in a EnterpriseArchive will cause the data to be stored in a different 'classloader'.

           

          Try using a WebArchive instead, see if that helps

          • 2. Re: jacoco.exec file is almost empty
            lenn.angel

            Hi !

             

            First of all thanks ! Your answer helped me to correct all my problems.

             

            1) First I changed my EAR to a WAR :

             

            WebArchive war = ShrinkWrap.create(WebArchive.class);

            war.addPackages(true, greetingPackage);

            war.addAsLibraries(frameworkLib);

             

            But with jacoco core 0.5.4.201111111111 I've got :

             

            Caused by: org.jboss.shrinkwrap.api.exporter.ArchiveExportException: java.lang.NoSuchMethodError: org.jacoco.core.instr.Instrumenter.<init>(Lorg/jacoco/core/runtime/IRuntime;)V

             

            2) Switch jacoco back to 0.5.3.201107060350

             

            Now deployment fails because of the "@EJB" annotation :

             

            java.lang.RuntimeException: beanInterface specified, interface com.cs.frameworktests.integration.greeting.ejb.GreetingManager, is not unique within EJB GreetingManagerFacade

             

            3) Specify interface class

             

            changed "@EJB" to "@EJB(beanInterface=GreetingManagerRemote.class)"

             

            Now everything works fine and I've got some coverage data in jacoco.exec :

            - com/cs/frameworktests/integration/greeting/ejb/GreetingManagerFacade

            - com/cs/frameworktests/integration/greeting/GreetingTest

             

            But it seems that the classes contained in the library isn't covered correctly...

             

            4) Changed WAR to JAR :

             

            JavaArchive jar = ShrinkWrap.create(JavaArchive.class);

            jar.addPackages(true, "com/cs/framework");

            jar.addPackages(true, greetingPackage);

             

            Now everything works fine and I've got some coverage data in jacoco.exec :

             

            - com/cs/frameworktests/integration/greeting/GreetingTest

            - com/cs/frameworktests/integration/greeting/ejb/GreetingManagerFacade

            - com/cs/framework/MyClassToCover

             

             

            Success !!

            • 3. Re: jacoco.exec file is almost empty
              aslak

              Currently it's not instrumenting classes in nested Archives. Got a fix for this coming in Alpha2

              1 of 1 people found this helpful