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

FP on S2187 when test class is a JUnit4 test class also inheriting from a JUnit3 TestCase

    Details

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

      Description

      When a JUnit4 test class is a subtype of a JUnit3 TestCase, then implementation of RSPEC-2187 raises a FP, saying that the test class does not contain unit tests... while it's an obvious FP.

      Required files to reproduce:

      import org.junit.Test;
      
      public class ReproducerTest extends AbstractJUnit4Test {  // FP HERE
        @Test public void shouldFoo() { assertTrue(1 + 1 == 2); }
      }
      
      import org.junit.Rule;
      import org.junit.rules.TestRule;
      import org.junit.runner.RunWith;
      import org.junit.runners.JUnit4;
      import org.junit.runners.model.Statement;
      
      @RunWith(JUnit4.class)
      public abstract class AbstractJUnit4Test extends AbstractJUnit3TestCase {
      
        @Rule
        public TestRule rule = (base, description) -> new Statement() {
          @Override
          public void evaluate() throws Throwable {
            String name = description.getMethodName();
            setName(name.startsWith("test") ? name : "test" + name);
            setUp();
            try {
              base.evaluate();
            } finally {
              tearDown();
            }
          }
        };
      }
      
      import junit.framework.TestCase;
      
      public abstract class AbstractJUnit3TestCase extends TestCase {
      
        @Override
        protected void setUp() throws Exception { System.out.println("setUp"); }
      
        @Override
        protected void tearDown() throws Exception { System.out.println("tearDown"); }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quentin.jaquier Quentin Jaquier
                Reporter:
                michael.gumowski Michael Gumowski
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: