rspec/rules/S2924/java/rule.adoc

75 lines
1.8 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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[]