rspec/rules/S2924/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

75 lines
1.8 KiB
Plaintext

== Why is this an issue?
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
[source,java]
----
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
[source,java]
----
public class ProjectDefinitionTest {
@Test
public void shouldSetKey() {
ProjectDefinition def = ProjectDefinition.create();
def.setKey("mykey");
assertThat(def.getKey(), is("mykey"));
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this unused "TestRule".
'''
== Comments And Links
(visible only on this page)
=== on 12 May 2015, 14:25:37 Ann Campbell wrote:
\[~david.gageot] this may not be as broad as you wanted...?
Also, I'm not quite sure about the impact of the @Rule annotation, so I didn't mention it in the description but left it in the code sample you provided. What happens if the annotation is missing?
=== on 12 May 2015, 14:54:12 David Gageot wrote:
If the annotation is missing, it will not do anything either. So this is really dead code
=== on 12 May 2015, 14:54:16 David Gageot wrote:
lgtm
endif::env-github,rspecator-view[]