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

56 lines
1.3 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
Assertion methods are throwing a \"``++java.lang.AssertionError++``". If this call is done within the try block of a try-catch cathing a similar error, you should make sure to test some properties of the exception. Otherwise, the assertion will never fail.
=== Noncompliant code example
[source,java]
----
@Test
public void should_throw_assertion_error() {
try {
throwAssertionError();
Assert.fail("Expected an AssertionError!"); // Noncompliant, the AssertionError will be caught and the test will never fail.
} catch (AssertionError e) {}
}
private void throwAssertionError() {
throw new AssertionError("My assertion error");
}
----
=== Compliant solution
[source,java]
----
assertThrows(AssertionError.class, () -> throwAssertionError());
----
[source,java]
----
try {
throwAssertionError();
Assert.fail("Expected an AssertionError!"); // Compliant, we made sure to test that the correct error is raised
} catch (AssertionError e) {
Assert.assertThat(e.getMessage(), is("My assertion error"));
}
----
== Resources
* https://github.com/junit-team/junit4/wiki/Exception-testing[JUnit 4 exception testing documentation]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]