google / gwtmockito

Better GWT unit testing
https://google.github.io/gwtmockito
Apache License 2.0
157 stars 50 forks source link

JaCoCo support not working #65

Open jsoltes opened 7 years ago

jsoltes commented 7 years ago

Jacoco code coverage tool should be supported since the version 1.1.5 (implemented by @rsauciuc ). However when we run jacoco on the sample project in this project, we are getting zero coverage. We are using following configuration in the parent pom:

<plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>${version.jacoco.plugin}</version>
          <executions>
            <execution>
              <id>default-prepare-agent</id>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
              <configuration>
                <propertyName>surefire.argLine</propertyName>
              </configuration>
            </execution>
            <execution>
              <id>default-report</id>
              <phase>prepare-package</phase>
              <goals>
                <goal>report</goal>
              </goals>
            </execution>
            <execution>
              <id>default-check</id>
              <goals>
                <goal>check</goal>
              </goals>
              <configuration>
                <haltOnFailure>${jacoco.haltOnFailure}</haltOnFailure>
                <rules>
                  <rule>
                    <element>BUNDLE</element>
                    <limits>
                      <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>${jacoco.line.coveredratio.minimum}</minimum>
                      </limit>
                    </limits>
                  </rule>
                </rules>
              </configuration>
            </execution>
          </executions>
</plugin>
nukesz commented 7 years ago

I experience the same thing when I use the @RunWith(GwtMockitoTestRunner.class), JaCoCo will not consider that test execution and report as 0% coverage. The GwtMockitoTestRunner uses javassist.Loader class for bytecode manipulation and as far as I know JaCoCo does not work if the class under test has been modified during runtime. My feeling is that GwtMockitoTestRunner has never worked with JaCoCo (regardless what the change log says).

ekuefler commented 7 years ago

I'm not particularly familiar with JaCoCo - @rsauciuc, can you comment if it is/was working for you?

The only change in GwtMockito to support JaCoCo is to ensure that we use the standard classloader for everything under the org.jacoco package. Classes under test will go through the GwtMockito classloader to do things like stub native methods and unfinalize some methods, so if that process interferes with JaCoCo there might not be a fix. But we should leave the bytecode of normal methods untouched.

rsauciuc commented 7 years ago

At the time I made the fix it was working for us, we were using JaCoCo in offline mode, doing bytecode instrumentation at build time.

In a different project, we had a similar problem with the agent competing with other instrumentation to make changes to the class files, and we switched the flag (classdumpdir, I think) to write out the class files after they were transformed, so JaCoCo's report task could match the binary coverage data with the actual classes.

jsoltes commented 7 years ago

I have set jacoco to do the offline instrumentation and it is working even for classes annotated @RunWith(GwtMockitoTestRunner.class). So far this is the only way to make it work I know about.

nukesz commented 7 years ago

Thanks @jsoltes , with offline instrumentation it works as excepted and shows the coverage. Maybe it will help for others, this is how I enabled it with maven:

    <properties>
        <jacocoVersion>0.7.9</jacocoVersion>
        <jacoco.skip.instrument>true</jacoco.skip.instrument>
    </properties>

    <dependencies>
        ...
        <dependency>
            <!--  must be on the classpath  -->
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <classifier>runtime</classifier>
            <version>${jacocoVersion}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacocoVersion}</version>
                <executions>
                    <execution>
                        <id>jacoco-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                        <configuration>
                            <skip>${jacoco.skip.instrument}</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                        <configuration>
                            <skip>${jacoco.skip.instrument}</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

To execute without coverage

mvn clean install

With coverage (and instrument):

mvn clean install -Djacoco.skip.instrument=false
JonCook commented 7 years ago

Any idea how to do this for ant? Thanks

zeitwesen commented 4 years ago

Should this still work with GWT 2.8.2 and GwtMockito + plain old JUnit Tests? Somehow do not get it to work at all. Does not even create the jacoco.exec file ("Skipping JaCoCo execution due to missing execution data file."). Anybody managed to find a solution?