rspec/rules/S5958/java/rule.adoc

43 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Unlike similar AssertJ methods testing exceptions (``++assertThatCode()++``, ``++assertThatExceptionOfType()++``, ...), the ``++assertThatThrownBy()++`` method can be used alone, failing if the code did not raise any exception.
2021-02-02 15:02:10 +01:00
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.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when ``++assertThatThrownBy++`` is used, without testing the exception further.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,java]
----
assertThatThrownBy(() -> shouldThrow()); // Noncompliant, is it really the exception you expected?
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[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[]