Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2924

JUnit rules should be used

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this unused "TestRule".
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Test Sources

      Description

      While some TestRule classes have the desired effect without ever being directly referenced by a test, several others do not, and there's no reason to leave them cluttering up the file if they're not in use.

      This rule raises an issue when Test class fields of the following types aren't used by any of the test methods: TemporaryFolder, and TestName.
      This rule also applies to the JUnit 5 equivalent classes: TempDir, and TestInfo.

      Noncompliant Code Example

      public class ProjectDefinitionTest {
      
        @Rule
        public TemporaryFolder temp = new TemporaryFolder();  // Noncompliant
      
        @Test
        public void shouldSetKey() {
          ProjectDefinition def = ProjectDefinition.create();
          def.setKey("mykey");
          assertThat(def.getKey(), is("mykey"));
        }
      }
      

      Compliant Solution

      public class ProjectDefinitionTest {
      
        @Test
        public void shouldSetKey() {
          ProjectDefinition def = ProjectDefinition.create();
          def.setKey("mykey");
          assertThat(def.getKey(), is("mykey"));
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: