Sonar:为此添加一些测试 class

Sonar: Add some tests to this class

我有一个实用程序 class,我在其中为我的单元测试创​​建假日期,我把它放在我的对象的测试部分:

public class FakeDataToTest {
  //.. fake objects
}

但是Sonar给了一个Blocker,因为这个class没有包含单元测试,也就是说没有方法注解@Test,sonar的消息是:

Add some tests to this class.

避免这种情况的干净方法是什么?

我在 sonar doc 中找到了解决方案,它说:

There's no point in having a JUnit TestCase without any test methods. Similarly, you shouldn't have a file in the tests directory with "Test" in the name, but no tests in the file. Doing either of these things may lead someone to think that uncovered classes have been tested.

This rule raises an issue when files in the test directory have "Test" in the name or implement TestCase but don't contain any tests.

Supported frameworks:

  • JUnit3
  • JUnit4
  • JUnit5
  • TestNG
  • Zohhak
  • ArchUnit

你所要做的就是使用一个没有Test的class名称,在我的例子中我使用了FakeData,否则它将被视为一个单元测试。

如果您对此 class 编写测试不感兴趣,您可以通过添加与规则 ID(您可以从声纳配置中获取)对应的 @SupressWarnings 注释来抑制声纳规则:

@java.lang.SuppressWarnings("squid:<rule_id>")