Sonar how to run unit test successfully - java

I have a project built with maven and I recently integrated Sonar... It is really easy to configure Sonar to analyze you're project but I couldn't configure it to run my project unit test also. I tried something with Jacoco but I get some Seam error and all the other tests are skipped. By the way I'm using TestNG to run tests manually.

You can use the relevant Analysis Parameters of sonar to reuse the test reports from your earlier run. You would set sonar.dynamicAnalysis property to reuseReports and specify the location of the reports in sonar.jacoco.reportPath or sonar.surefire.reportPath based on how you run the tests.
By the way, mvn sonar:sonar invokes maven's test goal, which runs unit tests as part of the analysis. So ideally if your maven can run unit tests, sonar should be able to run them.

Sonar cannot run tests, it can only analyze testing reports.
You can run yourself JUnit ( using Maven or Ant for exemple ) and push reports to Sonar (try Sonar's Maven plugin for that)
or you can give yourself a build factory (try hudson for exemple) and plug it to sonar.

Related

Verify jacoco covarage tests using power mockito without mvn command

Is there a way to verify the coverage of jacoco in eclipse without run mvn test command? I'm using power mock, so Eclemma does not work in my case.
Try a plugin e.g. EclEmma
https://www.eclemma.org/
Fast develop/test cycle: Launches from within the workbench like JUnit test runs can directly be analyzed for code coverage.
Rich coverage analysis: Coverage results are immediately summarized and highlighted in the Java source code editors.
Non-invasive: EclEmma does not require modifying your projects or performing any other setup.

Sonar cube not showing the test coverage percentage for Java project properly

I have a spring-based java project and I have my tests placed in /src/test/java folder. I tried to run the Sonar cube locally via
docker run -d --name sonarqube -p 9000:9000 sonarqube
in order to analyze the percentage of the test coverage. The test coverage shows 0% even though I have many tests in my project. And the Number of Unit tests count shows as 8 though there are more than that. Here is the screenshot
And here is my general Settings
Does anyone has any idea how to configure Sonar cube to reflect the test coverage?
I faced the same situation when trying to use Sonar with Docker.
What worked for me was:
Make sure you have run your tests [You can use mvn test]
Make sure JaCoCo has generated the coverage report [In my case reports were generated in folder target\jacoco-report]
Set the following property pointing to your XML report
Sonar property to be set
Sonarqube and sonar scanner do not provide tooling to generate code coverage reports. This should be done with tools like jacoco for java or opencover for .NET.
You can then add the output of jacoco to the sonar scanner with the sonar.coverage.jacoco.xmlReportPaths property.
so you will basically need the following steps for your analysis:
Sonar scanner begin
Restore packages
Build application
Use code coverage tool to test and calculate the coverage for your app
Sonar scanner end

Cucumber tests are being skipped

I am using cucumber framework in my project. So for the reporting part, i need have started building the cucumber reports with jenkins.
So during the setup for the trail build i am facing the below issues.
All the tests are skipped, but not executing.
Can anyone have solution. I am happy to provide any other inputs required.
When using maven and cucumber you have two options to run the tests.
With maven: mvn clean install
With cucumber:-Dtest=Runnerclass test
Where Runnerclass is the class name of runner.
Include surefire plugin in your POM.xml to pick your test. if you want to run test as well, then use mvn clean install.
Refer
How to run Cucumber JVM Test in Jenkins

Sonar Java - Is there a way to enforce unit test coverage?

Currently I have Sonar setup to show unit test coverage which works great. Is there a way to enforce a certain percentage of unit test coverage for a project? For example if the coverage drops below 50%, I want it to be a sonar violation and for my build to fail.
Yes, it is possible. For example, if you use jenkins, you can configure and step in which you can choose the minimum coverage(JaCoCo Plugin).
In the jenkins job, you can pass the unit test, and take the coverage report of jacoco, and then, a step with the sonar plugin, but if the coverage is less than for instance 50%, the jenkins will show your build as failure.
You need to install this plugin Build Breaker the only purpose of this plugin is to break the build when new alerts raise in the analysis.

What is the process with Maven project to compile and test the code?

I have maven project imported in my eclipse. Now I need to start making changes to it and test it with the integration test (out of App server). Currently, the integration test is run out of server using openEJB container.
My basic question is, what is the regular process to compile, build and test with Maven?
mvn install
Maven -> Update Project.
Run my test from command line
Is it how it is done? I am specifically interested in knowing mvn install commands.
So should I do all three steps before I can test it?
Example: I just wanted to print something and see what is the output. For this I guess I need to do all these steps?
The openEJB container needs classes so it can load them.
There is a wonderful Maven quick-reference sheet at http://maven.apache.org/guides/MavenQuickReferenceCard.pdf
First, you should be aware that unit tests and integration tests are separate and are run from separate plugins and at separate parts of the maven lifecycles. Unit tests are run with surefire and integration tests are run with failsafe.
You want to run integration tests and the failsafe documentation says:
NOTE: when running integration tests, you should invoke maven with the (shorter to type too)
mvn verify
rather than trying to invoke the integration-test phase directly...
This is the best way to run integration tests directly in maven. It will run all the preceding steps necessary (eg: compile) in order to run the integration tests. It won't waste time doing an install because install happens immediately after verify.
But if you're running the tests locally, it may be a better idea to run your integration tests directly in your IDE. That will give you a much faster feedback loop.
If it is Eclipse project the most reasonable thing is to do everything not from command line but from Eclipse. Assuming you have m2e plugin installed, go to your_project->run as->Maven test and run it.
You dont need neither install nor package phase to run Maven tests, package will create a jar which is not needed for tests, install will copy this jar to local repo which is also useless. When Maven run tests it uses compiled classes from target dir and ignores project's jar if even it exists.
Yes, mvn isntall is the most popular option. It compiles, packages and tests your project.

Categories