Knowledge Base > DCover CLI > Using SonarQube with Diffblue Cover

Using SonarQube with Diffblue Cover

Diffblue Cover writes tests that can be reported as “code smells” by SonarQube. This page gives suggestions for reducing the reported “code smells” and other warnings in the SonarQube output.

Turning off SonarQube for all Diffblue tests using Diffblue Cover CLI

A simple way to turn off the warnings given by SonarQube is to use the --annotate-suppress-warnings option to add the @SuppressWarnings code annotation to all test methods written by Diffblue Cover.

For example:

--annotate-suppress-warnings=all

suppresses all SonarQube warnings by producing the code annotation:

@SuppressWarnings({"all"})

You can use this command when you write tests, as shown below:

dcover create --annotate-suppress-warnings=all

Turning off SonarQube for specific warnings using Diffblue Cover CLI

If you want to turn off a specific warning, you can use the warning code to do this. For example, to suppress warnings java:S1161 and java:S5785 enter the following:

--annotate-suppress-warnings=java:S1161,java:S5785

The codes for SonarQube warnings can be found in the SonarQube information about a warning. An example warning code is shown at the end of this page.

Turning off SonarQube for all Diffblue tests using SonarQube configuration

One simple solution is to turn off SonarQube analysis for all written tests. This can be achieved either through the SonarQube administration or via the SonarQube project configuration:

Turning off SonarQube for all Diffblue tests

Another solution is to turn off SonarQube analysis for generated tests. This can be achieved either through the SonarQube administration or via the SonarQube project configuration:

Via SonarQube administration

  1. Open either the Administration (for all projects) or Project Settings (for one project).
  2. Go to General Settings.
  3. Go to Analysis Scope.
  4. Add the following string to the Test File Exclusions: **/*DiffblueTest.java. This will exclude all DCover generated tests from analysis.

Turning off SonarQube for all Diffblue tests via the project build configuration

Edit the project configuration file (e.g. in your pom.xml or gradle config file) and add the following line to the project properties:

<sonar.test.exclusions>**/*DiffblueTest.java</sonar.test.exclusions>

Full details of turning off SonarQube for tests can be found in the SonarQube documentation.

Fine-Grained tuning using SonarQube configuration

Another option is to use SonarQube’s fine-grained tuning options to turn on/off certain kinds of warnings and paths.

An alternative is to create a sonar-project.properties file in the resources directory and build custom rules there. For example, the following:

sonar.issue.ignore.multicriteria=e1
sonar.issue.ignore.multicriteria.e1.ruleKey=java:S5785
sonar.issue.ignore.multicriteria.e1.resourceKey=**/*DiffblueTest.java

ignores the JUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion warning on all *DiffblueTest.java files.

Manually adding @SuppressWarnings to Diffblue unit tests

It is also possible to turn off warnings in generated code by adding the @SuppressWarnings annotation manually. (This can also be done automatically, see the Suppressing Warnings section above.)

This can be done at the class or method level. Adding the appropriate annotations to classes or methods can prevent extraneous warnings, and can be as fine-grained as desired. For example the following:

@Test
  @SuppressWarnings("java:S5785")
  public void testEquals5() {

suppresses all warnings for JUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion in the testEquals5 method.

Note that codes for the appropriate warnings from SonarQube can be found in the SonarQube information about a warning e.g. java:S5785, as shown below: