Skip to content

Commit 6cd5811

Browse files
authored
Add catchThrowableOfType enriched alternatives (#2410)
This introduces the following alternatives: * `catchException` * `catchIllegalArgumentException` * `catchIllegalStateException` * `catchIndexOutOfBoundsException` * `catchIOException` * `catchNullPointerException` * `catchReflectiveOperationException` * `catchRuntimeException`
1 parent 367d110 commit 6cd5811

19 files changed

+1579
-16
lines changed

src/main/java/org/assertj/core/api/Assertions.java

Lines changed: 221 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a
11601160
*
11611161
* If the provided {@link ThrowingCallable} does not raise an exception, an error is immediately thrown,
11621162
* in that case the test description provided with {@link AbstractAssert#as(String, Object...) as(String, Object...)} is not honored.<br>
1163-
* To use a test description, use {@link #catchThrowable(ThrowableAssert.ThrowingCallable)} as shown below:
1163+
* To use a test description, use {@link #catchThrowable(ThrowingCallable)} as shown below:
11641164
* <pre><code class='java'> // assertion will fail but "display me" won't appear in the error
11651165
* assertThatThrownBy(() -&gt; {}).as("display me")
11661166
* .isInstanceOf(Exception.class);
@@ -1316,7 +1316,7 @@ public static <T> ObjectAssert<T> assertWith(T actual, Consumer<T>... requiremen
13161316
* <p>
13171317
* This caught {@link Throwable} can then be asserted.
13181318
* <p>
1319-
* If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowableAssert.ThrowingCallable, Class)}.
1319+
* If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowingCallable, Class)}.
13201320
* <p>
13211321
* Example:
13221322
* <pre><code class='java'>{@literal @}Test
@@ -1331,7 +1331,7 @@ public static <T> ObjectAssert<T> assertWith(T actual, Consumer<T>... requiremen
13311331
*
13321332
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
13331333
* @return The captured exception or <code>null</code> if none was raised by the callable.
1334-
* @see #catchThrowableOfType(ThrowableAssert.ThrowingCallable, Class)
1334+
* @see #catchThrowableOfType(ThrowingCallable, Class)
13351335
*/
13361336
public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
13371337
return AssertionsForClassTypes.catchThrowable(shouldRaiseThrowable);
@@ -1372,14 +1372,230 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
13721372
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
13731373
* @param type The type of exception that the code is expected to raise.
13741374
* @return The captured exception or <code>null</code> if none was raised by the callable.
1375-
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
1375+
* @see #catchThrowable(ThrowingCallable)
13761376
* @since 3.9.0
13771377
*/
13781378
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
13791379
Class<THROWABLE> type) {
13801380
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
13811381
}
13821382

1383+
/**
1384+
* Allows catching an instance of {@link Exception}.
1385+
* <p>
1386+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1387+
* otherwise it checks that the caught {@link Throwable} is of type {@link Exception} and casts it making it convenient to perform subtype-specific assertions on it.
1388+
* <p>
1389+
* Example:
1390+
* <pre><code class='java'>
1391+
* Exception exception = catchException(() -&gt; {throw new Exception("boom!");});
1392+
* // assertions succeed
1393+
* assertThat(exception).hasMessage("boom!");
1394+
*
1395+
* // succeeds as catchException returns null when the code does not throw any exceptions
1396+
* assertThat(catchException(() -&gt; {})).isNull();
1397+
*
1398+
* // fails as the thrown instance is not an Exception
1399+
* catchException(() -&gt; {throw new Throwable("boom!");});</code></pre>
1400+
*
1401+
* @param shouldRaiseException The lambda with the code that should raise the exception.
1402+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1403+
* @see #catchThrowable(ThrowingCallable)
1404+
* @since 3.22.0
1405+
*/
1406+
public static Exception catchException(ThrowingCallable shouldRaiseException) {
1407+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseException, Exception.class);
1408+
}
1409+
1410+
/**
1411+
* Allows catching an instance of {@link RuntimeException}.
1412+
* <p>
1413+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1414+
* otherwise it checks that the caught {@link Throwable} is of type {@link RuntimeException} and casts it making it convenient to perform subtype-specific assertions on it.
1415+
* <p>
1416+
* Example:
1417+
* <pre><code class='java'>
1418+
* RuntimeException runtimeException = catchRuntimeException(() -&gt; {throw new RuntimeException("boom!");});
1419+
* // assertions succeed
1420+
* assertThat(runtimeException).hasMessage("boom!");
1421+
*
1422+
* // succeeds as catchRuntimeException returns null when the code does not throw any exceptions
1423+
* assertThat(catchRuntimeException(() -&gt; {})).isNull();
1424+
*
1425+
* // fails as the thrown instance is not a RuntimeException
1426+
* catchRuntimeException(() -&gt; {throw new Exception("boom!");});</code></pre>
1427+
*
1428+
* @param shouldRaiseRuntimeException The lambda with the code that should raise the exception.
1429+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1430+
* @see #catchThrowable(ThrowingCallable)
1431+
* @since 3.22.0
1432+
*/
1433+
public static RuntimeException catchRuntimeException(ThrowingCallable shouldRaiseRuntimeException) {
1434+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseRuntimeException, RuntimeException.class);
1435+
}
1436+
1437+
/**
1438+
* Allows catching an instance of {@link NullPointerException}.
1439+
* <p>
1440+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1441+
* otherwise it checks that the caught {@link Throwable} is of type {@link RuntimeException} and casts it making it convenient to perform subtype-specific assertions on it.
1442+
* <p>
1443+
* Example:
1444+
* <pre><code class='java'>
1445+
* NullPointerException nullPointerException = catchNullPointerException(() -&gt; {throw new NullPointerException("boom!");});
1446+
* // assertions succeed
1447+
* assertThat(nullPointerException).hasMessage("boom!");
1448+
*
1449+
* // succeeds as catchNullPointerException returns null when the code does not throw any exceptions
1450+
* assertThat(catchNullPointerException(() -&gt; {})).isNull();
1451+
*
1452+
* // fails as the thrown instance is not a NullPointerException
1453+
* catchNullPointerException(() -&gt; {throw new Exception("boom!");});</code></pre>
1454+
*
1455+
* @param shouldRaiseNullPointerException The lambda with the code that should raise the exception.
1456+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1457+
* @see #catchThrowable(ThrowingCallable)
1458+
* @since 3.22.0
1459+
*/
1460+
public static NullPointerException catchNullPointerException(ThrowingCallable shouldRaiseNullPointerException) {
1461+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseNullPointerException, NullPointerException.class);
1462+
}
1463+
1464+
/**
1465+
* Allows catching an instance of {@link IllegalArgumentException}.
1466+
* <p>
1467+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1468+
* otherwise it checks that the caught {@link Throwable} is of type {@link IllegalArgumentException} and casts it making it convenient to perform subtype-specific assertions on it.
1469+
* <p>
1470+
* Example:
1471+
* <pre><code class='java'>
1472+
* IllegalArgumentException illegalArgumentException = catchIllegalArgumentException(() -&gt; {throw new IllegalArgumentException("boom!");});
1473+
* // assertions succeed
1474+
* assertThat(illegalArgumentException).hasMessage("boom!");
1475+
*
1476+
* // succeeds as catchNullPointerException returns null when the code does not throw any exceptions
1477+
* assertThat(catchIllegalArgumentException(() -&gt; {})).isNull();
1478+
*
1479+
* // fails as the thrown instance is not an IllegalArgumentException
1480+
* catchIllegalArgumentException(() -&gt; {throw new Exception("boom!");});</code></pre>
1481+
*
1482+
* @param shouldRaiseIllegalArgumentException The lambda with the code that should raise the exception.
1483+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1484+
* @see #catchThrowable(ThrowingCallable)
1485+
* @since 3.22.0
1486+
*/
1487+
public static IllegalArgumentException catchIllegalArgumentException(ThrowingCallable shouldRaiseIllegalArgumentException) {
1488+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseIllegalArgumentException, IllegalArgumentException.class);
1489+
}
1490+
1491+
/**
1492+
* Allows catching an instance of {@link IOException}.
1493+
* <p>
1494+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1495+
* otherwise it checks that the caught {@link Throwable} is of type {@link IOException} and casts it making it convenient to perform subtype-specific assertions on it.
1496+
* <p>
1497+
* Example:
1498+
* <pre><code class='java'>
1499+
* IOException iOException = catchIOException(() -&gt; {throw new IOException("boom!");});
1500+
* // assertions succeed
1501+
* assertThat(iOException).hasMessage("boom!");
1502+
*
1503+
* // succeeds as catchIOException returns null when the code does not throw any exceptions
1504+
* assertThat(catchIOException(() -&gt; {})).isNull();
1505+
*
1506+
* // fails as the thrown instance is not an IOException
1507+
* catchIOException(() -&gt; {throw new Exception("boom!");});</code></pre>
1508+
*
1509+
* @param shouldRaiseIOException The lambda with the code that should raise the exception.
1510+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1511+
* @see #catchThrowable(ThrowingCallable)
1512+
* @since 3.22.0
1513+
*/
1514+
public static IOException catchIOException(ThrowingCallable shouldRaiseIOException) {
1515+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseIOException, IOException.class);
1516+
}
1517+
1518+
/**
1519+
* Allows catching an instance of {@link ReflectiveOperationException}.
1520+
* <p>
1521+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1522+
* otherwise it checks that the caught {@link Throwable} is of type {@link ReflectiveOperationException} and casts it making it convenient to perform subtype-specific assertions on it.
1523+
* <p>
1524+
* Example:
1525+
* <pre><code class='java'>
1526+
* ReflectiveOperationException reflectiveOperationException = catchReflectiveOperationException(() -&gt; {throw new ReflectiveOperationException("boom!");});
1527+
* // assertions succeed
1528+
* assertThat(reflectiveOperationException).hasMessage("boom!");
1529+
*
1530+
* // succeeds as catchReflectiveOperationException returns null when the code does not throw any exceptions
1531+
* assertThat(catchReflectiveOperationException(() -&gt; {})).isNull();
1532+
*
1533+
* // fails as the thrown instance is not an IOException
1534+
* catchReflectiveOperationException(() -&gt; {throw new Exception("boom!");});</code></pre>
1535+
*
1536+
* @param shouldRaiseReflectiveOperationException The lambda with the code that should raise the exception.
1537+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1538+
* @see #catchThrowable(ThrowingCallable)
1539+
* @since 3.22.0
1540+
*/
1541+
public static ReflectiveOperationException catchReflectiveOperationException(ThrowingCallable shouldRaiseReflectiveOperationException) {
1542+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseReflectiveOperationException, ReflectiveOperationException.class);
1543+
}
1544+
1545+
/**
1546+
* Allows catching an instance of {@link IllegalStateException}.
1547+
* <p>
1548+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1549+
* otherwise it checks that the caught {@link Throwable} is of type {@link IllegalStateException} and casts it making it convenient to perform subtype-specific assertions on it.
1550+
* <p>
1551+
* Example:
1552+
* <pre><code class='java'>
1553+
* IllegalStateException illegalStateException = catchIllegalStateException(() -&gt; {throw new IllegalStateException("boom!");});
1554+
* // assertions succeed
1555+
* assertThat(illegalStateException).hasMessage("boom!");
1556+
*
1557+
* // succeeds as catchReflectiveOperationException returns null when the code does not throw any exceptions
1558+
* assertThat(catchIllegalStateException(() -&gt; {})).isNull();
1559+
*
1560+
* // fails as the thrown instance is not an IOException
1561+
* catchIllegalStateException(() -&gt; {throw new Exception("boom!");});</code></pre>
1562+
*
1563+
* @param shouldRaiseIllegalStateException The lambda with the code that should raise the exception.
1564+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1565+
* @see #catchThrowable(ThrowingCallable)
1566+
* @since 3.22.0
1567+
*/
1568+
public static IllegalStateException catchIllegalStateException(ThrowingCallable shouldRaiseIllegalStateException) {
1569+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseIllegalStateException, IllegalStateException.class);
1570+
}
1571+
1572+
/**
1573+
* Allows catching an instance of {@link IndexOutOfBoundsException}.
1574+
* <p>
1575+
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
1576+
* otherwise it checks that the caught {@link Throwable} is of type {@link IndexOutOfBoundsException} and casts it making it convenient to perform subtype-specific assertions on it.
1577+
* <p>
1578+
* Example:
1579+
* <pre><code class='java'>
1580+
* IndexOutOfBoundsException indexOutOfBoundsException = catchIndexOutOfBoundsException(() -&gt; {throw new IndexOutOfBoundsException("boom!");});
1581+
* // assertions succeed
1582+
* assertThat(indexOutOfBoundsException).hasMessage("boom!");
1583+
*
1584+
* // succeeds as catchIndexOutOfBoundsException returns null when the code does not throw any exceptions
1585+
* assertThat(catchIndexOutOfBoundsException(() -&gt; {})).isNull();
1586+
*
1587+
* // fails as the thrown instance is not an IOException
1588+
* catchIndexOutOfBoundsException(() -&gt; {throw new Exception("boom!");});</code></pre>
1589+
*
1590+
* @param shouldRaiseIndexOutOfBoundException The lambda with the code that should raise the exception.
1591+
* @return The captured exception or <code>null</code> if none was raised by the callable.
1592+
* @see #catchThrowable(ThrowingCallable)
1593+
* @since 3.22.0
1594+
*/
1595+
public static IndexOutOfBoundsException catchIndexOutOfBoundsException(ThrowingCallable shouldRaiseIndexOutOfBoundException) {
1596+
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseIndexOutOfBoundException, IndexOutOfBoundsException.class);
1597+
}
1598+
13831599
/**
13841600
* Entry point to check that an exception of type T is thrown by a given {@code throwingCallable}
13851601
* which allows to chain assertions on the thrown exception.
@@ -1389,7 +1605,7 @@ public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Throw
13891605
* .isThrownBy(() -&gt; { throw new IOException("boom!"); })
13901606
* .withMessage("boom!"); </code></pre>
13911607
*
1392-
* This method is more or less the same of {@link #assertThatThrownBy(ThrowableAssert.ThrowingCallable)} but in a more natural way.
1608+
* This method is more or less the same of {@link #assertThatThrownBy(ThrowingCallable)} but in a more natural way.
13931609
*
13941610
* @param <T> the exception type.
13951611
* @param exceptionType the exception type class.

0 commit comments

Comments
 (0)