Albin
Albin

Reputation: 2069

How to give test coverage for private constructor of a final class in sonarqube?

I have a util class which is final and I have added one private constructor for hide the default public one. How I can get the coverage for this class in sonarqube with jacoco coverage report and build in Jenkins?

public final class Util {

      // My contructor
      private Util() {
          super();
     }
 }

Upvotes: 4

Views: 4616

Answers (2)

Godin
Godin

Reputation: 10574

According to JaCoCo changelog such private empty no-argument constructors are automatically filtered out starting from JaCoCo version 0.8.0. Changelog also notes:

Tools that directly read exec files and embed JaCoCo for this (such as SonarQube or Jenkins) will provide filtering functionality only after they updated to this version of JaCoCo.

Announcement of release of JaCoCo version 0.8.0 states:

Tools that directly read exec files (which is not a final report) and embed JaCoCo for generation of report will provide filtering functionality only after they updated to this version of JaCoCo. So please follow/wait/etc respective vendors such as

Reports generated by corresponding version (0.8.0) of integrations developed as part of JaCoCo project by us (Ant Tasks, Maven Plugin and Command Line Interface) provide filtering functionality.

As of today (30 Jan 2018):

Upvotes: 5

VonC
VonC

Reputation: 1325976

If you configure sonar to use cobertura (and not jacoco) for the code coverage, you could simply exclude that method from code coverage.

That seems easier than writing an artifical test case using reflection.

Upvotes: 0

Related Questions