Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude certain classes from being included in the code coverage? (Java)

I could already exclude the classes with sonar cube report by adding this in the tag of my POM file.

 <properties>
        <java.version>1.8</java.version>
        <sonar.exclusions> 
            /src/main/java/com/example/org/test/mainpackage/message/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/jwt/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/security/services/**/*
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/AuthRestAPIs.java
            ,
            /src/main/java/com/example/org/test/mainpackage/controller/TestRestAPIs.java
        </sonar.exclusions>
    </properties>

However the same code when run locally to test the code coverage is not working at all.

Where do I set this up programmatically with Maven & Spring Boot ?.

I could already exclude the classes I want via the

Settings > Build, Execution, Deployment > Excludes > [Class(es) to be excluded]

or through

Edit Configurations > Select Code Coverage tab > then adding the package or class I want to be excluded or include only in the code coverage report.

What I want is coding the classes to be excluded i.e in the pom file like I did with exclusion of code for sonar qube.

To make it short. How do I do this programmatically?.

Reason why I wanted to this programmatically is that, the above two steps I did to exclude the files from being reported in the code coverage is that no files was included in all of my project that I could push in the repository so that changes i made for exclusions will reflect in all those who will pull from the git repository. With no files or configurations added that I could push and reflect to all in the repo. THis means that my exclusions will only work locally and not work on others. Right?. So here I am asking for a way to do programmatically like putting the exclusions in a POM file.

like image 241
iamjoshua Avatar asked May 07 '19 06:05

iamjoshua


2 Answers

As you are using Maven, you need to configure your exclusions in the jacoco plugin, which is used to capture the coverage statistics and pass it on to sonar.

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>target/jacoco.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>target/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
</executions>
<configuration>
    <systemPropertyVariables>
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
    </systemPropertyVariables>
    <excludes>
        <exclude>snmaddula/app/domain/*.class</exclude>
        <exclude>snmaddula/app/exception/*.class</exclude>
        <exclude>snmaddula/app/filter/*.class</exclude>
        <exclude>snmaddula/app/App.class</exclude>
    </excludes>
</configuration>
</plugin>

References:

  • maven-jacoco-config
  • gradle-jacoco-config
like image 191
snmaddula Avatar answered Nov 12 '22 15:11

snmaddula


This is not the kind of answer that I was looking for really. But for now this will do for me. Still looking for a way to do this via the pom file, java or beans. thanks

Solution:

So if you did your exclusions in the Run Configuration of you project. That is through the 2nd step I mentioned in the question which is through

Edit Configuration > Code Coverage. enter image description here


And then actually add your exclusions. You would have just to tick the share text box and Click apply. The exclusions you made will be made available in your project directory under

.idea/runConfigurations/[the xml file of the exclusions you made and other run configurations]. enter image description here

And to be able to push this, just remove the

.idea in your .gitignore file in git. enter image description here


And then you'd be able to push the run configurations files in your repository for others to use. or export so that it won't have to be manually configure in the edit configurations of intellij.

PS: This might affect the coverage report in your Sonarqube. You have to undo this and unpush from your repo the local run configuration to be able to use your sonar qubes class exclusions. For some reason in mine, it skipped all the exclusions I made before doing this.

like image 30
iamjoshua Avatar answered Nov 12 '22 15:11

iamjoshua