False-positive for java:S2699 "Tests should include assertions"

Environment
SonarLint plugin 4.14.1.27745
InteliJ IDEA 2020.3.2

Problem
S2699 is raised when test gets runnable with assertion from another class in another file.

Example to reproduce with JUnit5
file 1:

class AssertionsInTest {

    @Test
    void compliantTest1() {
        getRunnableWithAssertion();
    }

    @Test
    void compliantTest2() {
        LocalRunnableSupplier.getRunnableWithAssertion();
    }

    @Test
    void compliantTest3() {
        LocalFileRunnableSupplier.getRunnableWithAssertion();
    }

    @Test
    void nonCompliantTest() {
        ExternalRunnableSupplier.getRunnableWithAssertion();
    }

    static Runnable getRunnableWithAssertion() {
        return () -> Assertions.assertTrue(true);
    }

    public static class LocalRunnableSupplier {
        public static Runnable getRunnableWithAssertion() {
            return () -> Assertions.assertTrue(true);
        }
    }

}

class LocalFileRunnableSupplier {
    public static Runnable getRunnableWithAssertion() {
        return () -> Assertions.assertTrue(true);
    }
}

file 2:

public class ExternalRunnableSupplier {
    public static Runnable getRunnableWithAssertion() {
        return () -> Assertions.assertTrue(true);
    }
}

Hi Artem,

Thanks for your report and welcome to the community. Unfortunately, in this rule, we’re not performing cross-file analysis and it leads to such FPs. This is a known limitation. A possible workaround could be to name this method from an external file to start with ‘assert’ and the rule will assume a call to that method as an assertion itself.

Regards,
Margarita

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