SonarQube Warnings
How to address SonarQube warnings related to Diffblue Cover
There are various SonarQube warnings that may be issues for tests created by Diffblue Cover. This page lists common warnings and how to address them, if they can be addressed. Note that there are specific ways to address SonarQube warnings in the Diffblue Cover Plugin and Diffblue Cover CLI. This page is for an overview of the warnings you may see and how they can be addressed with any version of Diffblue Cover.
Configurable Warnings
Refactor this method to reduce the number of assertions from X to less than N
CLI only: Use the --max-assertions-per-test=(N-1)
option to limit the number of assertions per test.
Add at least one assertion to this test
There are two common causes for this warning.
An option has been given to Diffblue Cover to keep partial tests (docs for plugin or CLI) or skeleton tests. In this case, tests without assertions are expected and the tests should be completed manually.
This is due to SonarQube failing to recognize certain assertion methods. Most likely, you have to do the following:
Add
org.springframework.test.web.servlet.ResultActions#andExpect
to thecustomAssertionMethods
parameter for ruleS2699
. This explicitly tells SonarQube to treatandExpect
as an assertion.
Test method naming conventions
By default, Diffblue Cover’s test method naming complies with the pattern [a-z][a-zA-Z0-9_]*
You can change the method naming template if you want to create test names without underscores, for example. See method name template for plugin or CLI.
Unresolvable Warnings
Note that these warnings are where SonarQube and Diffblue Cover have different beliefs about what is best. These cannot be easily adjusted in Diffblue Cover, so the best approach is to suppress these or use other approaches as described for the Diffblue Cover Plugin and Diffblue Cover CLI.
Change the assertion arguments to not compare dissimilar types
SonarQube may incorrectly flag this situation when testing that the equals
method of a class returns false
when called with differing types.
Swap these two arguments so they are in correct order: expected value, actual value
This is not expected to happen. If you see this file a support request with a concrete example of test code generated by Cover.
Replace these N tests with a parameterized test
Diffblue Cover currently does not support parameterized tests. This feature is on the roadmap. Stay tuned.
Long variables
Diffblue Cover prefers long, descriptive variable names which may have up to about 40 characters.
Local variable could be final
Local variables in tests generated by Diffblue Cover are never reassigned by construction. Diffblue Cover does not mark them using the final
keyword.
Unit test assertions should include message
Diffblue Cover does not generate such messages as they unnecessarily increase the verbosity and thus reduce the readability of the tests.
Comment required for field
Diffblue Cover does not add JavaDoc comments to field declarations in test classes as they unnecessarily increase the verbosity and thus reduce the readability of the tests.
Last updated
Was this helpful?