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"));
}
}