Skip to main content

Try/Catch fail

Using try/catch blocks with fail() calls to test for exceptions is an awkward outdated pattern. JUnit4 introduced the @Test(expected=...) annotation parameter to simplify this pattern, but it had its own limitations. An alternative @Rule ExpectedException approach was also available, but it was more verbose and more disconnected.

TryCatchFailTest.java
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

class TryCatchFailTest {

@Test
void expectException() {
try {
boom();
fail("Exception uncaught");
} catch (IllegalStateException e) {
// Catch before fail is called
}
}

private void boom() {
throw new IllegalStateException("boom!");
}

@Test
void expectNoException() {
try {
noBoom();
assertTrue(true, "No Exception");
} catch (IllegalStateException e) {
fail("Exception caught");
}
}

private void noBoom() {
}
}
warning

Using try/catch blocks with fail() calls is an outdated and less readable way to test for exceptions in JUnit 5. Instead, we should use the assertThatThrownBy() and assertDoesNotThrow methods, which are more concise and expressive.