"JUnit5 inner test classes should be annotated with @Nested" should not apply to abstract inner classes

Hello

here is a false positive for rule java:S5790. It makes no sense to annotate an abstract class as @Nested, so the rule should not trigger for an abstract inner class.

  • SonarQube version: 8.9.3 (build 48735)
  • Maven plugin version: 3.8.0.2131
  • SonarLint for Eclipse version: 5.9.0.31414
  • Minimal code sample to reproduce, with JUnit 5 as the only dependency, issue visible in both SonarQube and SonarLint. Note that I have the same issue with SonarLint connected to SonarQube 8.9.3 and when not connected to any server.
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class JavaS5790FalsePositive {

    abstract class AbstractNested { // we get "Add @Nested to this inner test class" here

        protected abstract String abstractMethod();

        @Test
        void test() {
            assertNotNull(abstractMethod());
        }
    }

    @Nested
    class ConcreteNested1 extends AbstractNested {

        @Override
        protected String abstractMethod() {
            return "1";
        }

        @Test
        void anotherTest() throws Exception {
        }
    }

    @Nested
    class ConcreteNested2 extends AbstractNested {

        @Override
        protected String abstractMethod() {
            return "2";
        }
    }
}

Thanks
Vivien

Hello Vivien,

You’re right, it makes no sense to raise in this case. I have created a ticket. Thank you for the suggestion.

Cheers,
Sebastian

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