codeql icon indicating copy to clipboard operation
codeql copied to clipboard

False positive, java/unused-reference-type and JUnit 5 classes

Open mikeb01 opened this issue 2 years ago • 2 comments

JUnit Tests using JUnit 5 are reported as java/unused-reference-type. This is because the JUnit 5 convention is to use package scoped class and method names. This rule should be checking for methods with JUnit annotations (e.g. @Test, @BeforeEach, @ParameterizedTest). The current check only looks for subsclassing of TestCase, which hasn't been applicable since JUnit 4.

Code samples or links to source code

Example test code that would be reported as unused.

class MyTest
{
    @Test
    void test()
    {
       // some test code.
    }
}

Example false positive. https://github.com/real-logic/aeron/security/code-scanning/1583

mikeb01 avatar Jan 25 '23 19:01 mikeb01

Thanks for the report. That looks indeed like a false positive.

aibaars avatar Jan 26 '23 09:01 aibaars

Hi there! Is there a workaround for this issue? Could be solved by adding the following check not dead.getAMethod() instanceof TestMethod and or not dead instanceof TestClass and?

Zalimben avatar Jul 31 '24 22:07 Zalimben