2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2020-06-30 12:48:07 +02:00
|
|
|
It is very easy to write incomplete assertions when using some test frameworks. This rule enforces complete assertions in the following cases:
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2021-01-27 13:42:22 +01:00
|
|
|
* 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
|
2020-06-30 12:48:07 +02:00
|
|
|
|
|
|
|
In such cases, what is intended to be a test doesn't actually verify anything
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
// 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()
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2020-06-30 12:48:07 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
// 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();
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2020-06-30 12:48:07 +02:00
|
|
|
|
|
|
|
Variable assignments and return statements are skipped to allow helper methods.
|
2020-06-30 14:49:38 +02:00
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
[source,java]
|
2020-06-30 12:48:07 +02:00
|
|
|
----
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
----
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
* Complete the assertion.
|
|
|
|
* Add a call to 'assertAll' after all 'assertThat'.
|
|
|
|
* Add one or more 'assertThat' before 'assertAll'.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|