False postive with java:S5979 when using RuleChain

Hi

We sometimes use the mockito rule wrapped in a Junit RuleChain and when doing so sonarcloud fails to identify the initialization of mocks and raises java:S5979.

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;
import org.junit.rules.RuleChain;

public class SomeTest {

    @Rule
    public RuleChain ruleChain = RuleChain.outerRule(MockitoJUnit.testRule(this)).around(new OtherRule());


    @Mock
    private SomeType someMock;

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

This is running on sonarcloud.io

Hello @Sebastien_Arod,

Unfortunately, I could not reproduce the issue with the snippet you pasted here (ie: no issue raised).

Could you share a minimal code example that actually triggers the issue?

Cheers,

Dorian

I’ll try to do that yes

@Dorian_Burihabwa The following minimal self contained code example generates a false positive on java:S5979 for me using SonarLint intellij plugin (version 6.4.1.42145) or SonarCloud

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;

public class RuleChainAndMockitoRuleTest {

    private class OtherRule extends ExternalResource {
    }

    interface AnyService {

    }

    @Rule
    public RuleChain ruleChain = RuleChain.outerRule(MockitoJUnit.testRule(this)).around(new OtherRule());

    @Mock
    private AnyService mocked;


    @Test
    public void test() {
        Assert.assertNotNull(mocked);
    }

}

@Dorian_Burihabwa were you able to reproduce with this sample?

Hello @Sebastien_Arod ,
I was able to confirm the issue with the new sample thanks for that.

A ticket has been created to fix the issue.

Great. Thanks!

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