Ignoring a line with Sonar

依然范特西╮ 提交于 2020-05-29 02:30:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!