英文:
Sonar: Add some tests to this class
问题
以下是翻译好的部分:
我有一个工具类,在其中为了进行单元测试创建了一些虚假数据,我将它放在对象的测试部分:
public class FakeDataToTest {
//.. 虚假对象
}
但是 Sonar 给出了一个阻塞问题,因为这个类中没有包含单元测试,换句话说,没有一个被标注为 @Test
的方法,Sonar 的消息是:
为这个类添加一些测试。
有什么干净的方法可以避免这个问题吗?
英文:
I have an util class where I create Fake date for my unit test, I put it in test part of my Object:
public class FakeDataToTest {
//.. fake objects
}
But Sonar give a Blocker, because this class doesn't contain unit test, in another word not have a method annotated @Test
, the message of sonar is:
Add some tests to this class.
What is the clean way to avoid this ?
答案1
得分: 3
我在sonar文档中找到了解决方案,它说:
> 拥有没有任何测试方法的JUnit测试案例是没有意义的。
> 同样,您不应该在测试目录中拥有一个文件,文件名中包含**“Test”**,但文件中没有测试。这两种情况可能会让某人认为未覆盖的类已经经过测试。
>
> 当测试目录中的文件以“Test”命名或实现了TestCase但不包含任何测试时,此规则会引发问题。
>
> 支持的框架:
>
> - JUnit3
> - JUnit4
> - JUnit5
> - TestNG
> - Zohhak
> - ArchUnit
您只需在类名中使用没有Test
的名称,就像我使用了FakeData
一样,否则它将被视为单元测试。
英文:
I found the solution in the sonar doc, it say:
> 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
All you should to do is to use a class name without Test
in my case I used FakeData
, else it will considered as a unit test.
答案2
得分: 0
如果您对为这个类编写测试不感兴趣,您可以通过添加与规则ID对应的@SupressWarnings注释(可以从您的Sonar配置中获取)来抑制Sonar规则:
@java.lang.SuppressWarnings("squid:<rule_id>")
英文:
If you are not interested in writing tests for this class, you can supress the Sonar rule by adding the @SupressWarnings annotation corresponding to the rule id (which you can fetch from your sonar configuration) :
@java.lang.SuppressWarnings("squid:<rule_id>")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论