Can I do these using Code Coverage with Cobertura - java

I am just looking at the cobertura maven plugin and I wasnt sure if the following is possible
Instrument classes
Run junit tests
Generate Cobertura report without reinstrumenting classes and running tests
I have a multi module maven project and the coverage of the domain module is showing up as 0% even though its been used by every other module
I have tried different combinations of things but the coverage of my domain module always stays at 0%.
People have mentioned writing separate tests for the domain classes but i dont want to do this as you could easily write a tests to test a function that isn't actually being used anywhere within the codebase
Any pointers would be greatly appreciated

In order to do so you would have to execute the maven goals in the correct order so :
cobertura:instrument
test
goalToAskCoberturaToGenerateReport
But then comes the trouble : there is no such goal as cobertura:report, if you look at the documentation and source code of the maven plugin : The goal cobertura:cobertura is the only goal generating the report. I suspect it is as such because of some maven internal limitation.
So in short, given the state of the maven plugin it is not possible.
You might have a chance to manage what you want to achieve by executing cobertura from the command line.

For multi-module maven projects cross-module coverage seems not to be available off-the-shelf with Cobertura.
A solution using a mixture of maven and ant is described byThomas Sundberg:
http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/
See also this related question:
Maven2 Multiproject Cobertura Reporting Problems During mvn site Build

Related

Why Surefire plugin is need in Maven?

I have used Maven for a while. In my previous projects, i did not create pom.xml from scratch, and I've seen Surefire plugin always already added to POM.
At first I thought Surefire is a must for Maven to be able to search for test java files to run, however even after I remove Surefire plugin from POM, Maven is still able to find the test cases under src/test/java.
So is the purpose of Surefire to add additional features such as in:
https://maven.apache.org/surefire/maven-surefire-plugin/
but not absolutely necessary to run "mvn test"?
Thanks
Very first line you doc you provided says:
". It generates reports in two different file formats:"
And reports are very important, I assume you must have seen reports after running test to check coverage and other stats.
You have answer somewhere in your question itself :)
I was also searching for reasons why so many plugins required such as Sure-fire and fail-safe..
found that we need surefire for "test" phase to execute the UNIT TEST of an application
which is the least minimal activity we do to after compiling our code therefore surefire is omnipresent.

How can I link production code and test code by using sonarQube, maven, or other tools?

I want to know how to link production code and test code, i.e., I'd like to answer this question: which product codes are the targets of this test code?
I'd like to do this automatically.
My project uses maven and I have used SonarQube and the source codes are written in Java.
If it's needed, I will try any other tools.
How can I link production codes and test codes?
Please let me know how to do it.
What you want to do is effectively getting the Coverage of your tests, an answer to the question "What lines/branches of my code are covered by my tests?".
Maven and SonarQube are perfectly suited for this, the only thing you need to add into the mix is Jacoco. A good explanation for the configuration of Jacoco/Junit is here. Jacoco is an agent that gets added to the execution of your tests and which monitors them, analyzing which lines/branches have been executed (covered) and which have not.
The important part is to configure the jacoco plugin and the surefire/failsafe plugin(s) (last one is for integration tests) to use jacoco. This will generate jacoco report files, which then can/will be read by SonarCube during the sonar:sonar goal (you might have to set the path to these files either in your maven pom.xml as a sonar property or directly in the SonarQube server properties, both work fine).
You can test it step-by-step, first getting jacoco to run, since it already creates nice html reports. Reading the reports into SonarQube is the easier part then.

How to collect statistics about java integration tests

My development environment includes Maven, Failsafe and Spring testing IS (AbstractJUnit4SpringContextTests).
I'm looking for a way to collect statistics about the integration tests run.
Information such as Test duration, process memory, etc...
What is the best way to collect such information with the above configuration (and to integrate with the maven flow).
as far as i recall, the surefire plugin is able to write reports to some directory (which will be used by e.g. jenkins to present results)
There is a maven plugin for Sonar. It is highly configurable, maybe solution to your problem.
The failsafe plugin generates a xml file with the tests result. this can be used, for example by a jenkins plugin.

How to get the full code coverage on a Maven multi-modules project

Imagine a multi-modules Maven project, such as the following one:
parent
+- core
+- main
main is dependent on the core module.
I now write a class CoreClass in core, with 2 methods: method1() and method2().
In core tests, I write a test class that will only test CoreClass.method1().
If I run a coverage tool (in my case Cobertura, using mvn sonar:sonar), I will find that I get 50% of test coverage on CoreClass (if we imagine that both methods have the same length).
Until now, everything is ok.
Now, in main project, I write a test class that will test the CoreClass.method2(). So normally, I would expect to have 100% of line coverage on CoreClass when I run an analysis on the whole project.
However, I still get my 50%.
I understand that this is a comprehensive behavior. Indeed, Cobertura will instrument CoreClass for coverage analysis only during the tests execution on the core module, and not on the main.
That explains why I still have 50% of code coverage.
However, my question is to know if there is a way to get the real code coverage of CoreClass when I am running the tests on all of my modules.
Thanks!
ps: I know that in a perfect world, it is not the concern of the main module to test the core classes. But as you may know, we are not in a perfect world :o)
Technical information: Java 1.6, JUnit 4.8.1, Maven 2.0.9 (will be upgraded to 2.2.1 soon, but I don't think it does really matter), Sonar 2.8
Use jacoco and sonar and have a single jacoco.exec file result for all modules.
Sonar will use this file and report the correct coverage for each module.
I have use it for a multi module project successfully with Sonar
Here you can find a solution for jacoco/sonar and here only for jacoco.

Sonar project integration

Hi Ladies and Gentlemen,
We've quite big project with own build framework, based mostly on Java (however other languages exist).
We'd like to use Sonar Hudson plugin to graphically present various code metrics.
How do we do this?
Do we need to change project structure and bring it to maven or there is a workaround to just specify where to get test results and other artifacts from?
Thank you
The method we are using is this:
we built a custom pom.xml build file specific for sonar (we are using ant for other build purposes)
it only has to perform test well, so specified hardcoded dependency references with
<scope>system</scope>
we didn't change the project structure for maven, you can specify in maven custom scr, test, resources directories (as long as you have only one src and test directory)
the command used in CI is
mvn clean compile sonar:sonar
We are using Continuum for the CI part, but it should work just as well in Hudson.
This method did not change any other build items, it's just custom made for Sonar. But it does open the way for a Continuous Integration (daily) build, or for using maven as a build tool. This method is similar to the "sonar light mode" described here
More information here:
http://docs.sonarqube.org/display/SONAR/Documentation
http://docs.codehaus.org/display/SONAR/Continuous+Integration
You can use sonar without Maven. you just have to tell it where the rport files are with properties: sonar.cobertura.reportPath, sonar.clover.reportPath, sonar.surefire.reportsPath…
See here: http://sonar.codehaus.org/tag/ant/
There's a tick box to set these values when configuring the build in hudson - it is called "Check if this project is NOT built with maven2"

Categories