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

73 lines
1.7 KiB
Plaintext

== Why is this an issue?
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
[source,java]
----
@Test
public void testMethod() {
try {
// Some code
} catch (MyException e) {
Assert.fail(e.getMessage()); // Noncompliant
}
}
----
=== Compliant solution
[source,java]
----
@Test
public void testMethod() throws MyException {
// Some code
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this failure assertion and simply add the exception type to the method signature.
=== Highlighting
* Primary: ``++Assert.fail++``
* Secondary: ``++try++`` and ``++catch (...)++``
'''
== Comments And Links
(visible only on this page)
=== is related to: S3431
=== on 27 Jun 2016, 22:53:57 Alix Lourme wrote:
@[~ann.campbell.2] : I will update the https://github.com/SonarSource/sonar-java/pull/902[PR] with your last modifications ASAP (I need update an https://github.com/SonarSource/orchestrator/pull/1[orchestrator PR] before) ; but IMO a remediation cost to 1min (perhaps 2) would be better than 5 (this is a default very quick to fix).
=== on 29 Jun 2016, 21:30:35 Ann Campbell wrote:
Just now saw this [~axel3rd]. Remediation updated to 2min.
endif::env-github,rspecator-view[]