问题
Sonar complains about a line.
Thread.sleep(SLEEP_TIME); // NOSONAR
Its problem is that
"Thread.sleep" should not be used in tests
Using Thread.sleep in a test is just generally a bad idea. It creates brittle tests that can fail unpredictably depending on environment ("Passes on my machine!") or load.
And it makes sense, this should be fixed. But my problem is: why doesn't the NOSONAR part has any effect here? It seems to work in other parts of the code where it's used, e.g. with
public static final String PASSWORD_FILE_NAME = "secret.txt"; // NOSONAR
it doesn't complain any more that there is a hardcoded password in the code. So why doesn't it work with the Thread.sleep() case?
I can see the issue both in SonarQube and in the SonarLint plugin for IntelliJ.
回答1:
You are basically hitting this problem : https://jira.sonarsource.com/browse/SONARJAVA-1113
Which was that the // NOSONAR was not taken into account in tests.
This has been fixed in the latest release of the sonar java plugin release (3.11)
(On a side note, using NOSONAR is not great IMO, you should keep track of issue you don't want to fix using SonarQube rather than cluttering your code with comments that are linked to a specific external tool)
来源:https://stackoverflow.com/questions/35741259/ignoring-a-line-with-sonar