2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-25 14:18:12 +02:00
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.
2020-12-21 15:38:52 +01:00
2023-05-25 14:18:12 +02:00
=== 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"));
}
----
2020-12-21 15:38:52 +01:00
2023-05-03 11:06:20 +02:00
== Resources
2020-12-21 15:38:52 +01:00
* https://github.com/junit-team/junit4/wiki/Exception-testing[JUnit 4 exception testing documentation]
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]