Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate catchThrowableOfType(ThrowingCallable, Class) in favor of catchThrowableOfType(Class, ThrowingCallable) #2823

Merged
merged 19 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 48 additions & 3 deletions assertj-core/src/main/java/org/assertj/core/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ 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 @@ -1371,13 +1371,15 @@ 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);
}

/**
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
* <p>
* 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
Expand Down Expand Up @@ -1415,11 +1417,54 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @see #catchThrowable(ThrowingCallable)
* @since 3.9.0
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
return AssertionsForClassTypes.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.23.11
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable);
}
/**
* Allows catching an instance of {@link Exception}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,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 Down Expand Up @@ -897,13 +897,15 @@ 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);
}

/**
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
scordio marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* 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,
Expand Down Expand Up @@ -940,11 +942,53 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.9.0
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
return ThrowableAssert.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(() -&gt; { throw new CustomParseException("boom!", 1, 5); },
* CustomParseException.class);
scordio marked this conversation as resolved.
Show resolved Hide resolved
* // 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.23.11
*/
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
54 changes: 51 additions & 3 deletions assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,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 @@ -1858,7 +1858,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 All @@ -1867,6 +1867,8 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
}

/**
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
* <p>
* 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
Expand Down Expand Up @@ -1905,9 +1907,55 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
*
* @since 3.20.0
*/

@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
return AssertionsForClassTypes.catchThrowableOfType(shouldRaiseThrowable, type);
return AssertionsForClassTypes.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(() -&gt; { throw new TextException("boom!", 1, 5); }, RuntimeException.class);</code></pre>
scordio marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.23.11
*/
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class<THROWABLE> type,
ThrowingCallable shouldRaiseThrowable) {
return AssertionsForClassTypes.catchThrowableOfType(type, shouldRaiseThrowable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,8 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
}

/**
* @deprecated use {@link #catchThrowableOfType(Class, ThrowingCallable)} instead.
* <p>
* 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,
Expand Down Expand Up @@ -1299,10 +1301,51 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
* @see #catchThrowable(ThrowableAssert.ThrowingCallable)
* @since 3.9.0
*/
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable, Class<THROWABLE> type) {
return ThrowableAssert.catchThrowableOfType(shouldRaiseThrowable, type);
return ThrowableAssert.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.23.11
*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
}

@SuppressWarnings("unchecked")
@Deprecated
public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(ThrowingCallable shouldRaiseThrowable,
Class<THROWABLE> type) {
Throwable throwable = catchThrowable(shouldRaiseThrowable);
scordio marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -77,4 +78,14 @@ public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Throw
.isInstanceOf(type);
return (THROWABLE) throwable;
}
@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);
return (THROWABLE) throwable;
}
}