rspec/rules/S2970/java/rule.adoc
Fred Tingaud 51369b610e
Make sure that includes are always surrounded by empty lines (#2270)
When an include is not surrounded by empty lines, its content is inlined
on the same line as the adjacent content. That can lead to broken tags
and other display issues.
This PR fixes all such includes and introduces a validation step that
forbids introducing the same problem again.
2023-06-22 10:38:01 +02:00

86 lines
2.2 KiB
Plaintext

== Why is this an issue?
It is very easy to write incomplete assertions when using some test frameworks. This rule enforces complete assertions in the following cases:
* Fest: ``++assertThat++`` is not followed by an assertion invocation
* AssertJ: ``++assertThat++`` is not followed by an assertion invocation
* Mockito: ``++verify++`` is not followed by a method invocation
* Truth: ``++assertXXX++`` is not followed by an assertion invocation
In such cases, what is intended to be a test doesn't actually verify anything
=== Noncompliant code example
[source,java]
----
// Fest
boolean result = performAction();
// let's now check that result value is true
assertThat(result); // Noncompliant; nothing is actually checked, the test passes whether "result" is true or false
// Mockito
List mockedList = Mockito.mock(List.class);
mockedList.add("one");
mockedList.clear();
// let's check that "add" and "clear" methods are actually called
Mockito.verify(mockedList); // Noncompliant; nothing is checked here, oups no call is chained to verify()
----
=== Compliant solution
[source,java]
----
// Fest
boolean result = performAction();
// let's now check that result value is true
assertThat(result).isTrue();
// Mockito
List mockedList = Mockito.mock(List.class);
mockedList.add("one");
mockedList.clear();
// let's check that "add" and "clear" methods are actually called
Mockito.verify(mockedList).add("one");
Mockito.verify(mockedList).clear();
----
=== Exceptions
Variable assignments and return statements are skipped to allow helper methods.
[source,java]
----
private BooleanAssert check(String filename, String key) {
String fileContent = readFileContent(filename);
performReplacements(fileContent);
return assertThat(fileContent.contains(key)); // No issue is raised here
}
@Test
public void test() {
check("foo.txt", "key1").isTrue();
check("bar.txt", "key2").isTrue();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Complete the assertion.
* Add a call to 'assertAll' after all 'assertThat'.
* Add one or more 'assertThat' before 'assertAll'.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]