rspec/rules/S2924/java/rule.adoc

61 lines
1.3 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)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]