Skip to content

Commit

Permalink
Deprecate catchThrowableOfType(ThrowingCallable, Class) in favor of…
Browse files Browse the repository at this point in the history
… `catchThrowableOfType(Class, ThrowingCallable)` (#2823)

Co-authored-by: vlsi@users.noreply.github.com
  • Loading branch information
java-coding-prodigy committed May 10, 2024
1 parent 69980de commit a33478f
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 51 deletions.
52 changes: 49 additions & 3 deletions assertj-core/src/main/java/org/assertj/core/api/Assertions.java
Expand Up @@ -1368,7 +1368,8 @@ public static <T> ObjectAssert<T> assertWith(T actual, Consumer<T>... requiremen
* <p>
* This caught {@link Throwable} can then be asserted.
* <p>
* If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowingCallable, Class)}.
* If you need to assert on the real type of Throwable caught (e.g. IOException), use
* {@link #catchThrowableOfType(Class, ThrowingCallable)}.
* <p>
* Example:
* <pre><code class='java'>{@literal @}Test
Expand All @@ -1383,7 +1384,7 @@ public static <T> ObjectAssert<T> assertWith(T actual, Consumer<T>... requiremen
*
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowableOfType(ThrowingCallable, Class)
* @see #catchThrowableOfType(Class, ThrowingCallable)
*/
public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
return AssertionsForClassTypes.catchThrowable(shouldRaiseThrowable);
Expand Down Expand Up @@ -1426,10 +1427,55 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowingCallable)
* @since 3.9.0
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
return catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
* Allows catching a {@link Throwable} of a specific type.
* <p>
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
* otherwise it checks that the caught {@link Throwable} has the specified type and casts it making it convenient to perform subtype-specific assertions on it.
* <p>
* Example:
* <pre><code class='java'> class TextException extends Exception {
* int line;
* int column;
*
* public TextException(String msg, int line, int column) {
* super(msg);
* this.line = line;
* this.column = column;
* }
* }
*
* TextException textException = catchThrowableOfType(() -&gt; { throw new TextException("boom!", 1, 5); },
* TextException.class);
* // assertions succeed
* assertThat(textException).hasMessage("boom!");
* assertThat(textException.line).isEqualTo(1);
* assertThat(textException.column).isEqualTo(5);
*
* // succeeds as catchThrowableOfType returns null when the code does not thrown any exceptions
* assertThat(catchThrowableOfType( Exception.class, () -&gt; {})).isNull();
*
* // fails as TextException is not a RuntimeException
* catchThrowableOfType(() -&gt; { throw new TextException("boom!", 1, 5); }, RuntimeException.class);</code></pre>
*
* @param <THROWABLE> the {@link Throwable} type.
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowingCallable)
* @since 3.26.0
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
Expand Down
Expand Up @@ -798,7 +798,7 @@ public static <T extends Throwable> AbstractThrowableAssert<?, T> assertThat(T a

/**
* Entry point to check that an exception of type T is thrown by a given {@code throwingCallable}
* which allows to chain assertions on the thrown exception.
* which allows chaining assertions on the thrown exception.
* <p>
* Example:
* <pre><code class='java'> assertThatExceptionOfType(IOException.class)
Expand All @@ -822,7 +822,7 @@ public static <T extends Throwable> ThrowableTypeAssert<T> assertThatExceptionOf
* <pre><code class='java'>assertThatNoException().isThrownBy(() -&gt; { System.out.println("OK"); });</code></pre>
*
* This method is more or less the same of {@code assertThatCode(...).doesNotThrowAnyException();} but in a more natural way.
*
* @return the created {@link NotThrownAssert}.
* @since 3.17.0
*/
Expand All @@ -831,7 +831,7 @@ public static NotThrownAssert assertThatNoException() {
}

/**
* Allows to capture and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
* Allows capturing and then assert on a {@link Throwable} more easily when used with Java 8 lambdas.
*
* <p>
* Example :
Expand Down Expand Up @@ -898,7 +898,7 @@ public static NotThrownAssert assertThatNoException() {
*
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see AssertionsForClassTypes#catchThrowableOfType(ThrowableAssert.ThrowingCallable, Class)
* @see AssertionsForClassTypes#catchThrowableOfType(Class, ThrowableAssert.ThrowingCallable)
*/
public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowable(shouldRaiseThrowable);
Expand Down Expand Up @@ -940,12 +940,55 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.9.0
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
return catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
* Allows catching a {@link Throwable} of a specific type.
* <p>
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null,
* otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it,
* making it convenient to perform subtype-specific assertions on the result.
* <p>
* Example:
* <pre><code class='java'> class CustomParseException extends Exception {
* int line;
* int column;
*
* public CustomParseException(String msg, int l, int c) {
* super(msg);
* line = l;
* column = c;
* }
* }
*
* CustomParseException e = catchThrowableOfType(CustomParseException.class,
* () -&gt; { throw new CustomParseException("boom!", 1, 5); });
* // assertions pass
* assertThat(e).hasMessageContaining("boom");
* assertThat(e.line).isEqualTo(1);
* assertThat(e.column).isEqualTo(5);
*
* // fails as CustomParseException is not a RuntimeException
* catchThrowableOfType(RuntimeException.class,
* () -&gt; { throw new CustomParseException("boom!", 1, 5); });</code></pre>
*
* @param <THROWABLE> the {@link Throwable} type.
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.26.0
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
}
// -------------------------------------------------------------------------------------------------
// fail methods : not assertions but here to have a single entry point to all AssertJ features.
// -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1007,7 +1050,7 @@ public static void fail(Throwable realCause) {
/**
* Only delegate to {@link Fail#failBecauseExceptionWasNotThrown(Class)} so that Assertions offers a full feature
* entry point to all AssertJ features (but you can use Fail if you prefer).
*
* <p>
* {@link Assertions#shouldHaveThrown(Class)} can be used as a replacement.
*
* @param throwableClass the Throwable class that was expected to be thrown.
Expand Down Expand Up @@ -1875,7 +1918,7 @@ public static void setLenientDateParsing(boolean value) {
* User date formats are used before default ones in the order they have been registered (first registered, first
* used).
* <p>
* AssertJ is gonna use any date formats registered with one of these methods :
* AssertJ is going to use any date formats registered with one of these methods :
* <ul>
* <li>{@link org.assertj.core.api.AbstractDateAssert#withDateFormat(String)}</li>
* <li>{@link org.assertj.core.api.AbstractDateAssert#withDateFormat(java.text.DateFormat)}</li>
Expand Down Expand Up @@ -1921,7 +1964,7 @@ public static void registerCustomDateFormat(DateFormat userCustomDateFormat) {
* User date formats are used before default ones in the order they have been registered (first registered, first
* used).
* <p>
* AssertJ is gonna use any date formats registered with one of these methods :
* AssertJ is going to use any date formats registered with one of these methods :
* <ul>
* <li>{@link org.assertj.core.api.AbstractDateAssert#withDateFormat(String)}</li>
* <li>{@link org.assertj.core.api.AbstractDateAssert#withDateFormat(java.text.DateFormat)}</li>
Expand Down
54 changes: 49 additions & 5 deletions assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java
Expand Up @@ -1851,7 +1851,7 @@ public static ListAssert<Integer> then(IntStream actual) {

/**
* Creates a new instance of <code>{@link SpliteratorAssert}</code> from the given {@link Spliterator}.
*
* <p>
* Example:
* <pre><code class='java'> Spliterator&lt;Integer&gt; spliterator = Stream.of(1, 2, 3).spliterator();
* then(spliterator).hasCharacteristics(Spliterator.SIZED); </code></pre>
Expand All @@ -1870,7 +1870,7 @@ public static <ELEMENT> SpliteratorAssert<ELEMENT> then(Spliterator<ELEMENT> act
* <p>
* This caught {@link Throwable} can then be asserted.
* <p>
* If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(ThrowingCallable, Class)}.
* If you need to assert on the real type of Throwable caught (e.g. IOException), use {@link #catchThrowableOfType(Class, ThrowingCallable)}.
* <p>
* Example:
* <pre><code class='java'>{@literal @}Test
Expand All @@ -1885,7 +1885,7 @@ public static <ELEMENT> SpliteratorAssert<ELEMENT> then(Spliterator<ELEMENT> act
*
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowableOfType(ThrowingCallable, Class)
* @see #catchThrowableOfType(Class, ThrowingCallable)
*
* @since 3.20.0
*/
Expand Down Expand Up @@ -1929,12 +1929,56 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowingCallable)
*
* @since 3.20.0
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
return catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
* Allows catching a {@link Throwable} of a specific type.
* <p>
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown it returns null
* otherwise it checks that the caught {@link Throwable} has the specified type and casts it making it convenient to perform subtype-specific assertions on it.
* <p>
* Example:
* <pre><code class='java'> class TextException extends Exception {
* int line;
* int column;
*
* public TextException(String msg, int line, int column) {
* super(msg);
* this.line = line;
* this.column = column;
* }
* }
*
* TextException textException = catchThrowableOfType(TextException.class() ,
* -&gt; { throw new TextException("boom!", 1, 5); });
* // assertions succeed
* assertThat(textException).hasMessage("boom!");
* assertThat(textException.line).isEqualTo(1);
* assertThat(textException.column).isEqualTo(5);
*
* // succeeds as catchThrowableOfType returns null when the code does not thrown any exceptions
* assertThat(catchThrowableOfType(Exception.class, () -&gt; {})).isNull();
*
* // fails as TextException is not a RuntimeException
* catchThrowableOfType(RuntimeException.class, () -&gt; { throw new TextException("boom!", 1, 5); });</code></pre>
*
* @param <THROWABLE> the {@link Throwable} type.
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowingCallable)
* @since 3.26.0
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
Expand Down
Expand Up @@ -1301,11 +1301,53 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.9.0
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class<THROWABLE> type) {
return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
return catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
* Allows catching a {@link Throwable} of a specific type.
* <p>
* A call is made to {@code catchThrowable(ThrowingCallable)}, if no exception is thrown {@code catchThrowableOfType} returns null,
* otherwise it checks that the caught {@link Throwable} has the specified type then casts it to it before returning it,
* making it convenient to perform subtype-specific assertions on the result.
* <p>
* Example:
* <pre><code class='java'> class CustomParseException extends Exception {
* int line;
* int column;
*
* public CustomParseException(String msg, int l, int c) {
* super(msg);
* line = l;
* column = c;
* }
* }
*
* CustomParseException e = catchThrowableOfType(CustomParseException.class,
* () -&gt; { throw new CustomParseException("boom!", 1, 5); });
* // assertions pass
* assertThat(e).hasMessageContaining("boom");
* assertThat(e.line).isEqualTo(1);
* assertThat(e.column).isEqualTo(5);
*
* // fails as CustomParseException is not a RuntimeException
* catchThrowableOfType(RuntimeException.class,
* () -&gt; { throw new CustomParseException("boom!", 1, 5); });</code></pre>
*
* @param <THROWABLE> the {@link Throwable} type.
* @param shouldRaiseThrowable The lambda with the code that should raise the exception.
* @param type The type of exception that the code is expected to raise.
* @return The captured exception or <code>null</code> if none was raised by the callable.
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.26.0
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type, ThrowingCallable shouldRaiseThrowable) {
return ThrowableAssert.catchThrowableOfType(type, shouldRaiseThrowable);
}
// -------------------------------------------------------------------------------------------------
// fail methods : not assertions but here to have a single entry point to all AssertJ features.
// -------------------------------------------------------------------------------------------------
Expand Down
Expand Up @@ -67,14 +67,21 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
return null;
}

@SuppressWarnings("unchecked")
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return catchThrowableOfType(type, shouldRaiseThrowable);
}

@SuppressWarnings("unchecked")
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
Throwable throwable = catchThrowable(shouldRaiseThrowable);
if (throwable == null) return null;
// check exception type
new ThrowableAssert(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create())
.isInstanceOf(type);
new ThrowableAssert<>(throwable).overridingErrorMessage(shouldBeInstance(throwable, type).create())
.isInstanceOf(type);
return (THROWABLE) throwable;
}

}

0 comments on commit a33478f

Please sign in to comment.