Test coverage missing in Netbeans 8.1? - java

I downloaded recently Netbeans 8.1 here
I chose second option: "Java EE".
But I can't find how to run test coverage for my unit tests. I have this menu:
It's a Maven Web Application.
When I go to Tools -> Plugins and search for "coverage", I have this:
I installed it and restarted the IDE, I saw it was installing the plugin but there's no change in my menu. If I search "coverage" in the Installed plugins, nothing shows up else than the one I just installed... I thought Netbeans had it implemented? I also thought Netbeans has the Maven test coverage as well...
I read that the plugin I installed (TikiOne JaCoCoverage) is just an extension of the already existing Netbeans Test Coverage.. so that would explain why I can't see it.
How can I enable test coverage?
Thanks.

you should add the JaCoCo plugin into <plugins> section of your pom.xml file.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
After you build the project, the menu item for Code Coverage menu item appears when you right-click the project.
Finally, you can select Show Report from the menu. Everything is described here.

This is unfortunately little documented, but for me the menu entries appeared when I added the JaCoCo Maven plugin by hand:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</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>
<rules><!-- implementation is needed only for Maven 2 -->
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits><!-- implementation is needed only for Maven 2 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.01</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
The maven goal verify runs the coverage report. You'll also have the window for coverage as mentioned in the official docs.
Unfortunately the plugin or the integration seems a little buggy, since you can either run the tests and see its results in the Test Results NB window, OR see the coverage... there seems to be two ways of running tests and I haven't found a way to do both at the same time yet.

Keep in mind that after you install the plugin and add this info in your pom, you might be able to see the option code coverage when you right click in a package.
However, as javaeeeee's answer says, you SHOULD BUILD your project again in order to see the actual coverage.

Related

How to specify Maven Goals' Parameter value

http://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html
I am like really not familiar with maven at all. And the project that I am working on requires it....
I am trying to customize this Jacoco tool in maven. Especially the "include" parameter for prepare-agent goal. I am testing a big project with about 4000 classes in many different packages. But the only coverage information that I need is only from 5-10 classes.
Any idea how I can specify something like this? Basically specify "include" while running test. Or do I have to specify it in the POM file?
"mvn jacoco:prepare-agent -Dinclude = "weka.associations.Apriori" test"
yes you can specify in the pom.xml file
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<configuration>
<excludes>
<exclude>**/*_.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin
where exclude tag will include your exclusion list , the classes which you want yo exclude to get code coverage, right now , it will not exclude anything
Kindly use the new version of jacoco as it is old one which i have specified

JaCoCo with Maven - missing execution data file

We have a Maven multi module project consisting of a parent (HelloWorld) and different children (HelloWorldServices and HelloWorldPresentation) and use Jenkins to build.
The error after running the successful test is
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) # HelloWorldServices ---
[INFO] Skipping JaCoCo execution due to missing execution data file:/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
The lines before it says
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-cli) # HelloWorldServices ---
[INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
This is how I defined the parent pom JaCoCo plugin:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destfile>${project.artifactId}/target/jacoco.exec</destfile>
<datafile>${project.artifactId}/target/jacoco.exec</datafile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
In no pom did I explicitly mention surefire. I also tried what you find everywhere to put the argLine in the configuration but all with the same result. The JaCoCo .exec file has never been created, no matter what I do. As for the goals, I use
mvn clean install jacoco:prepare-agent jacoco:report
Since when I omit the jacoco goals, it doesn't even display the INFO message.
You should not invoke the agent after the install phase but before, so instead of invoking:
mvn clean install jacoco:prepare-agent jacoco:report
You should invoke
mvn clean jacoco:prepare-agent install jacoco:report
The main reason is: the agent will not participate to the build lifecycle, the test phase will already be executed as part of the install phase, then Maven will execute the agent as per command line invocation, but it will be too late.
You should probably also change the plugin configuration above to:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Note: I removed the configuration section, because it was actually pointing to default values. Moreover, XML elements are case sensitives here, so your datafile element was simply ignored, it should have been dataFile instead. The same applies to destFile.
The prepare-agent goal is already using ${project.build.directory}/jacoco.exec as default destFile value, the same applied to the dataFile value for the report goal. The main reason for this change would be a more flexible and standard build, not relying on artifactId as project name (the default, but still not mandatory) and using the more generic ${project.build.directory} property instead to point directly at target.
Final note: make sure to configure the Jacoco Plugin executions within the build/plugins section and not build/pluginManagement/plugins section. The pluginManagement section is meant for governance and common harmonization of versions or configurations, but it will be ignored if the corresponding plugin would not be declared under build/plugins.
As per official Maven POM reference
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.
(note: bold is mine)
JaCoCo reports get created from the execution data file.
If this file is not present then JaCoCo report goal skips the report creation.
So it is compulsory to create the execution data file.
Reasons due to which execution data file will not get created are the following
- Tests are not present.
- All tests are ignored.
- Surefire plugin is missing.
- JaCoCo's prepare-agent goal is not executed, which sets argLine which is needed to configure to surefire.
- Surefire plugin is not configured with JaCoCo's agent.
I think that "destfile" and "datafile" are case sensitive so try to replace them with "destFile" and "dataFile", maybe it'll work :)
I just dealt with this issue today and found out that this happening when I set the argLine parameter in Surefire plugin (which I needed to do for running JavaFx-related tests) which replaces the default even after you have prepare-agent in your jacoco-maven-plugin goals.
So basically, the solution is to add the original argLine and append your additional arg lines using #{argLine} as noted here:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>#{argLine} --enable-native-access ALL-UNNAMED --add-modules jdk.incubator.foreign</argLine>
</configuration>
</plugin>
It mentioned in that post to assign the jacoco agent argline variable ${surefire.argLine} but i think those steps are not necessary (at least not in my case).
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<target>
<propertyfile file="lombok.config">
<entry key="config.stopBubbling" value="true" />
<entry key="lombok.addLombokGeneratedAnnotation" value="true" />
</propertyfile>
</target>
<excludes>
<exclude>**/domain/**</exclude>
<exclude>**/enumeration/**</exclude>
<exclude>**/exception/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- Add this checking -->
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>65%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I share with you my response may be someone else work for him
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<!-- <configuration>
<excludes>
<exclude>package you want exclude</exclude>
</excludes>
</configuration>-->
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- <execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.9</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>-->
</executions>
</plugin>

Code coverage of client/server web application

I am writing a multi-module application. Some of the modules are just basic Java libraries which are then included in the WAR of a webapp.
I would like to run code coverage in the following scenario:
I am running the webapp through an embedded Jetty that is started via Maven.
I have tests which are executing HTTP requests against the webapp.
I would like to get code covered in the webapp and also by the tests.
Is this possible and how can it be achieved with Cobertura, JaCoCo or Emma? From what I understand, the code coverage will only cover the client-side code in this scenario. Am I correct?
I think if you would manage to attach the JaCoCo-agent to the jvm that runs the jetty, it should be able to measure which code has been called over the time you run the integration tests against your webapp. So you should get a statistic that shows you the code coverage.
There is a JaCoCo Maven Plugin - though I'm not sure if this will help with you scenario. Just used it during unit tests.
Edit: found a blog-post that seems to point in the right direction here
Measure Code Coverage by Integration Tests with Sonar
Here's how I achieved it
Assuming you already have a minimal pom.xml config:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</
<version>0.7.4.201502262128</vers
</plugin>
Download JaCoCo's agent and copy jacocoagent.jar to a suitable location (e.g. $HOME/tools/jacocoagent0.7.4.jar)
Attach JaCoCo's agent to Maven's JVM via:
export MAVEN_OPTS="$MAVEN_OPTS \
-javaagent:$HOME/tools/jacocoagent0.7.4.jar=output=tcpserver,port=6300"
Run your application with embedded jetty server e.g. mvn jetty:run
Run your integration tests
In another shell, dump and report via mvn jacoco:dump jacoco:report
Open your report on ./target/site/index.html (by default)
You can use Jacoco plugin to generate code coverage Here is the plugin configuration I used for junit test code coverage.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.10.201208310627</version>
<configuration>
<skip>${maven.test.skip}</skip>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Note: you may get life cycle not covered error in maven while using eclipse, one way is you explicitly mention the life cycle using plugin management. I installed the jacoco plugin from the market place which resolved my problem
We had a similar scenario where integration test were run on a jetty server. Also we needed a combined report for all the tests unit and integration. The solution we implemented was to run-forked jetty and pass the jvmargs with the jacoco javaagent details. Our code coverage reports covered all the rest api's and the service layer java code.
The pom config for jacoco
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<id>prepare-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>prepare-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
With the above config we generated a common exec file for both unit and integration test. Next we configured jetty to run-forked
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin.version}</version>
<configuration>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<webApp>
<contextPath>/myway</contextPath>
<descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
</webApp>
<!-- passing the jacoco plugin as a jvmarg -->
<jvmArgs>${failsafeArgLine}</jvmArgs>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<configuration>
<daemon>true</daemon>
<waitForChild>false</waitForChild>
</configuration>
<goals>
<goal>run-forked</goal>
</goals>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
This would launch jetty in a separate jvm with the jvmargs. Finally we generated the report in the reporting tag of the pom. We noticed that adding the report to the build plugins did not capture the integration tests run by the jetty.
<reporting>
</plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<reportSets>
<reportSet>
<id>jacoco-report</id>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
The reports can be accessed at target/site/jacoco/index.html, alternately you can run it from the command line.
mvn jacoco:report
Hope it helps.

Jacoco, Maven & TestNG (0 test coverage)

I'm currently trying to run Jacoco using Maven and TestNG.
I haven't downloaded/installed anything.
I've tried adding the following to my parent POM (also tried adding to test package POM, but still no results)
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<configuration>
<destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</destfile>
<datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
When I do "mvn clean install" or mvn package the output is appearing in root folder as "test-output". I've tried setting "outputDirectory" in Maven but this does nothing.
Another bigger issue I'm having is it's not picking up any testNG tests. The code coverage is 0.
Can someone help please?
Why not add testNG dependency to your pom?
Do you have any classes with names ending <MyClass>Test?
You need to annotate one of the methods of that class with #Test (obvious for you). But make sure you specify public accessor.

Emma does not display uncovered packages in the report

I am using maven with emma to generate coverage report on linux red hat. After I run command mvn emma:emma, packages which are not covered by JUnit tests are not displayed in the report.
I am using following configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
</plugin>
Any idea what is going on?
Or any way to make sure all packages including uncovered are part of report?
note this was supposed to be a comment, and It doesn't answer not answer the question directly. It questions the usefulness of the question.
Quit using emma and start using jacoco. AFAIK, emma has been dormant since around 2007. The latest version of the emma plugin on maven central is from 2010.
As of today (fist quarter 2013). There is a version on maven central from feb-2013.
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco</artifactId>
<version>0.6.2.201302030002</version>
</dependency>
And it is synchronized with the maven plugin. Here is an example of a configuration of mine
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/entities/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
The reports from jacoco also look nicer than the ones in emma.
Compare:
with

Categories

Resources