Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3731

S5786 should not report on abstract classes or overridding test methods

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.15
    • Component/s: Rules
    • Labels:

      Description

      S5786 currently reports on abstract classes containing test methods.
      This is not practical as these abstract classes and their methods could have non-compliant visibility modifiers to enable inheritance and composition in test code.

      S5786 should be modified to allow for this specific cases:

      1. The visibility modifiers of abstract classes and methods should be ignored
      2. The visibility modifiers of test methods annotated with @Override and @Test should be ignored

      As a consequence, the following code should not trigger any report from S5786.

      public abstract class AbstractTest {
      
        @Test
        protected void test_inherited() {
          assertEquals(42, 21 * 2);
        }
      
        protected abstract void test_abstract();
      
        @Test
        public void test_to_override() {
          assertEquals(12, 23 - 11);
        }
      }
      
      interface InterfaceTest {
        void test_to_implement();
      
        @Test
        default void default_test() {
          assertEquals(49, 7 * 7);
        }
      }
      
      
      class ChildTest extends AbstractTest implements InterfaceTest {
      
        @Test
        @Override
        public void test_to_implement() {
          assertEquals("aaa", "AAA".toLowerCase(Locale.ROOT));
        }
      
        @Test
        @Override
        protected void test_abstract() {
          assertTrue("   ".trim().isEmpty());
        }
      
        @Override
        @Test
        public void test_to_override() {
          super.test_to_override();
          assertEquals(23, 12 + 11);
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dorian.burihabwa Dorian Burihabwa
                Reporter:
                dorian.burihabwa Dorian Burihabwa
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: