Using Jacoco in pom.xml + SonarQube - testing

I'm doing some tests with jUnit and now I have to check the code coverage with Jacoco (adding it at pom.xml) and SonarQube.
And this is where I don't know what I have to do with Jacoco or how to add it to pom.xml and later view it with SonarQube.

The best is to check this sample code showing how to have UT, IT and coverage using Jacoco: https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco and this documentation will help : http://docs.sonarqube.org/display/PLUG/Code+Coverage+by+Integration+Tests+for+Java+Project

Related

Converting current project into Cucumber

We already have a working project in test NG and now we are trying to add cucumber framework to it.
Sofar, I could goto eclipse marketplace and download the cucumber plugin, also added cucumber dependencies into my gradle file.
Then I have created a feature file - login.feature and its corresponding step definition for it. In the same feature file I have right clicked -> run as -> (selected) cucumber feature, now the issue is eclipse is unable to recognize the step definition for its corresponding feature file and the executed console in the attached screenshots.
Output:
0 Scenarios
0 Steps
0m0.567s
Please see the attached screenshots for more information.
If you just need to see whether feature file is running, first you need to make sure that Cucumber Eclipse Plugin is properly installed, Eclipse needs to be restarted after installation. If you are using TestNG then Cucumber-TestNG and Cucumber-Java dependencies of same version should be used. I could see Cucumber-TestNG dependency missing. You need to exclude JUnit also.
Then in order to run feature file, you first need to configure it, by selecting Run Configuration->click Cucumber feature(left panel)->select project->provide Feature file location->Apply and Run
You can also create a Test Runner which will link Feature file with Step definition and run the test as TestNG test from Runner.
Eg. Test Runner
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
#CucumberOptions(features = "src/test/resources/featureFiles", glue = "stepDefinitions")
public class RunTestNGTest extends AbstractTestNGCucumberTests {
}

Is there a way to remove jacoco branch coverage in IntelliJ

I have a spring boot project in IntelliJ, and I want to remove the jacoco branch coverage for this project, how can I do that?
figured that out, just add these lines to pom.xml file, you can configure by youself
<jacoco.coverage.line>0.80</jacoco.coverage.line>
<jacoco.coverage.branch>0.80</jacoco.coverage.branch>

JaCoCo configuration when sources and tests classes are in different modules

I have a multi-module project with Gradle(2.2) + JaCoCo + Sonar. I'm using the sonar-runner plugin, and when I execute the tests, I can see in each module the test report under build/jacoco/jacoco.exec. So far so good.
The problem is, I have some tests in module A that are testing classes from other module B, and so that JaCoCo is identifying that classes from module B with 0% code-coverage. I know this is not a good practice but it has to be done like that.
Example of the structure:
moduleA
src
java
Foo
test
moduleB
src
java
test
TestFoo
Then JaCoCo will show the class Foo with 0% coverage. I have tried merging the results from all modules but I get the same result but in one single file, so this is not what I'm looking for. Is there any option to include sources from other module when executing the JaCoCo report?
Thanks.
each module need its own tests. Jacoco build each jacoco.exec module after module and cannot go back to a previous one. So you have to set a TestFoo in moduleA.

How can I tell which specific version of JUnit IntelliJ is using?

I've started working on a large project where the IntelliJ environment has already been set up. The environment includes JUnit, and I can successfully run unit tests. I've seen screens where I can specify the usage of JUnit 3 or JUnit 4, but how can I determine which specific JUnit is being used to run my tests, e.g., JUnit 4.11?
I have already tried "Open Module Settings". When I look at the "Dependencies" tab, I don't see anything relating to JUnit, although I can run JUnit tests.
Which jar is used?
When you run JUnit from IntelliJ, the very first line of the console output displays your classpath. You can use Ctrl+F to find any "junit" references.
junit-rt.jar is a helper library that JetBrains might have written. By opening the jar as an archive with 7-zip, you will find that the only package inside it is under com.intellij
According to Java: Which of multiple resources on classpath JVM takes? the first reference to junit.jar is the one you will use.
What version is that jar?
Once you know which jar is being used, there are a number of ways to find the version. One is to use this code taken from https://stackoverflow.com/a/16729507/1405720
import junit.runner.Version;
System.out.println("JUnit version is: " + Version.id());
Another method might be to open up the jar as an archive and see if you can figure it out from there.
If you are looking for the JUnit libraries that are shipped with IntelliJ have a look at the corresponding jars in the lib/ directory of your Intellij IDEA installation.
For more information on this have a look at the online documention:
http://www.jetbrains.com/idea/webhelp/configuring-testing-libraries.html
http://www.jetbrains.com/idea/webhelp/testing.html

Jacoco and Arquillian in a multi module Maven project

I am following this article: http://www.softwarepassion.com/it-coverage-with-arquillian-jacoco-extension/ to get test coverage for arquillian integration tests. My project is a multi module though and I don't know where to put the plug in and dependencies. Is it in the top pom, the artifact-making module or in the integration test module?
Thank you
To some extent it depends on the details of your Maven setup, which aren't in your question. Here is some general advice.
1) Where should you put the arquillian-jacoco and jacoco dependencies?
These dependencies should probably go wherever the rest of your Arquillian dependencies are. My understanding is that it is simply having these dependencies that triggers Arquillian to use JaCoCo, not the plugin declaration; even if these dependencies are in a parent of the POM with the actual Arquillian tests, the Arquillian test classes should still be instrumented. You wouldn't put these dependencies in a sibling module to the module with the tests though as they need to be inherited by the integration test module (unless this sibling module has been declared as a dependency of the test module of course).
2) Where should you put the JaCoCo plugin declaration?
As noted above, you may not even need this declaration, depending on what you are trying to achieve. If you want to generate a report, rather than just the jacoco.exec files, then you will need to declare the plugin and an execution with the report goal. You may also want to declare the plugin with the prepare-agent goal if you have other tests that you wish to be instrumented with JaCoCo, such as unit tests.
If you are going to declare the plugin, it can be treated the same way as any other Maven plugin. If you want to run JaCoCo across multiple modules by default you could choose to put the plugin declaration in your parent POM within the regular 'plugins' tag and have it inherited by all child modules, or you may wish to put it in the parent POM within the 'pluginManagement' element so the configuration can be inherited (see http://maven.apache.org/pom.html#Plugin_Management). Alternatively, if you only want to run Arquillian tests in your integration test module, you could also simply declare the plugin in this module's POM (given that you want a report, and without the prepare-agent goal if you're only instrumenting Arquillian tests).
Hope that helps!