# Using SonarQube with Cover CLI

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` use 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](#manually-adding-suppresswarnings-to-diffblue-unit-tests).

## 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:

1. Open **Administration** (for all projects) or **Project Settings** (for one project).
2. Go to **General Settings** > **Analysis Scope** and add `**/*DiffblueTest.java` to the **Test File Exclusions**. This will exclude all Cover 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](https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/).

## 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:

```groovy
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](#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:

```groovy
@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:

<div align="left"><figure><img src="/files/uxSbCunIW2fWsC4A2fE7" alt="" width="563"><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cover-docs.diffblue.com/features/cover-cli/cover-cli-admin/using-sonarqube-with-cover-cli.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
