JUnit BeforeEach methods must not be static

The following code raises java:S2325 (“private” and “final” methods that don’t access instance data should be “static”):

final void doSometing() {
  // don't touch instance variables

This is a false positive because @BeforeEach and @AfterEach methods must not be static. Is this a known issue?

Hello @sithmein

The rule java:S2325 is targetting only main code, it is not supposed to be executed on test code. You should probably check your analysis parameters, more specifically sonar.sources and sonar.tests.

Hope it helps.

In our case this happens when using Maven. I would expect the Sonar Maven plug-in to set these variables correctly.

The Maven scanner should indeed correctly set these properties correctly by default and you should not have to do it by yourself.

Can I ask you to verify your analysis parameters (or analysis context), or share them, anonymized, and double-check that (even if it might sound completely obvious):

  • The file is correctly placed in a test folder, included in what property sonar.tests defines
  • There is no override of the property sonar.sources.inclusions which might include test folders?