rspec/rules/S5958/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

43 lines
1.2 KiB
Plaintext

== Why is this an issue?
Unlike similar AssertJ methods testing exceptions (``++assertThatCode()++``, ``++assertThatExceptionOfType()++``, ...), the ``++assertThatThrownBy()++`` method can be used alone, failing if the code did not raise any exception.
Still, only testing that an exception was raised is not enough to guarantee that it was the expected one, and you should test the exception type or content further. In addition, it will make explicit what you are expecting, without relying on side-effects.
This rule raises an issue when ``++assertThatThrownBy++`` is used, without testing the exception further.
=== Noncompliant code example
[source,java]
----
assertThatThrownBy(() -> shouldThrow()); // Noncompliant, is it really the exception you expected?
----
=== Compliant solution
[source,java]
----
assertThatThrownBy(() -> shouldThrow()).isInstanceOf(IOException.class);
//or
assertThatThrownBy(() -> shouldThrow()).hasMessage("My exception");
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Test further the exception raised by this assertThatThrownBy call.
=== Highlighting
Method name "assertThatThrownBy".
endif::env-github,rspecator-view[]