rspec/rules/S3658/java/rule.adoc

59 lines
1.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
When the code under test in a unit test throws an exception, the test itself fails. Therefore, there is no need to surround the tested code with a ``++try++``-``++catch++`` structure to detect failure. Instead, you can simply move the exception type to the method signature.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when there is a fail assertion inside a ``++catch++`` block.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:39 +02:00
Supported frameworks:
2020-06-30 12:48:39 +02:00
* JUnit3
* JUnit4
* JUnit5
* Fest assert
* AssertJ
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,java]
----
@Test
public void testMethod() {
try {
// Some code
} catch (MyException e) {
Assert.fail(e.getMessage()); // Noncompliant
}
}
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,java]
----
@Test
public void testMethod() throws MyException {
// Some code
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]