rspec/rules/S3658/java/rule.adoc

46 lines
900 B
Plaintext

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.
This rule raises an issue when there is a fail assertion inside a ``++catch++`` block.
Supported frameworks:
* JUnit3
* JUnit4
* JUnit5
* Fest assert
* AssertJ
== Noncompliant Code Example
----
@Test
public void testMethod() {
try {
// Some code
} catch (MyException e) {
Assert.fail(e.getMessage()); // Noncompliant
}
}
----
== Compliant Solution
----
@Test
public void testMethod() throws MyException {
// Some code
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]