1

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.

enter image description here

What is the clean way to avoid this ?

Pekka
  • 141
  • 2
  • 11

2 Answers2

3

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.

Pekka
  • 141
  • 2
  • 11
  • can you add link to the doc/quote? – Ori Marko Sep 13 '20 at 11:51
  • @user7294900 yes, check now – Pekka Sep 13 '20 at 12:27
  • If you also put Test file in test directory, rule make sense – Ori Marko Sep 13 '20 at 12:30
  • Yes this is the reason, I put the FakeDataTest in the test directory – Pekka Sep 13 '20 at 12:34
  • I believe, the idea is not to have a class inside `test` directory ending with the term `Test` which makes a perfect sense. **This rule raises an issue when files in the test directory are named *Test, *Tests, or *TestCase or implement TestCase but don't contain any tests.** – Prashant Sep 13 '20 at 12:35
  • @Prashant I don't know where I'm not clear, yes the problem happen because I put a class where the name is end with **Test** in the test which not contain any test! – Pekka Sep 13 '20 at 12:38
  • 1
    @Prashant test packages are for test related classes, like utilities and custom mocks. It's perfectly fine that `FakeData` is here, in its specific package – Luiggi Mendoza Nov 02 '20 at 13:41
0

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>")