False positive in java:S5979 / Mockito initialization

I think I may have stumbled across a false positive of the rule java:S5979 / Annotated Mockito objects should be initialized.

We use the mockito rule with additional configuration of strict stubs (see example). If I remove the strictness call the violation disappears.

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

public class SomeTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

    @Mock
    private SomeType someMock;

    @Test
    public void someTest() {
        // ...
    }
}

This is running against sonarcloud.io.

Hello @coyo_lh, thanks for reporting the issue.

I can confirm that this is a false positive that the current version of the check does not properly check the use of MockitoJUnit.rule() followed by options.

A ticket has been created to this effect.

Cheers,

Dorian

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