False positive on Java S5979 when using TestNG

Hey SonarSource team :kissing_heart:

I think I found a false positive for the rule 5979 - Annotated Mockito objects should be initialized

The rule is raised on the declaration of my mock in this code

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.testng.MockitoTestNGListener;

@Listeners(MockitoTestNGListener.class)
public class TestClass {
   
   @Mock
   private MyMockedClass mockedClass;

   @InjectMocks
   private MyClassWithInjectdMocks myClass;

  ...
}

I believe that using the @Listeners(MockitoTestNGListener.class) is the equivalent of @RunWith(MockitoJUnitRunner.class) when using JUnit and this rule should not be raised here.

with :heart:

Xavier

PS: Iā€™m using SonarLint for IntelliJ version 4.14.2.28348 :wink:

6 Likes

Hello @Xav,
Thanks for reporting the issue and welcome to the community :wink:
Indeed, the current implementation of the rule does not take TestNG annotations into account.
A ticket has been created to handle the issue.

Cheers,

Dorian

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.