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

Recommend assertThrows and assertFailsWith. #1293

Merged
1 commit merged into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions core/src/main/java/com/google/common/truth/ThrowableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
/**
* Propositions for {@link Throwable} subjects.
*
* <p>Truth does not provide its own support for calling a method and automatically catching an
* expected exception, only for asserting on the exception after it has been caught. To catch the
* exception, we suggest {@link org.junit.Assert#assertThrows(Class,
* org.junit.function.ThrowingRunnable) assertThrows} (JUnit), <a
* href="https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-fails-with.html">{@code
* assertFailsWith}</a> ({@code kotlin.test}), or similar functionality from your testing library of
* choice.
*
* <pre>
* InvocationTargetException expected =
* assertThrows(InvocationTargetException.class, () -> method.invoke(null));
* assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
* </pre>
*
* @author Kurt Alfred Kluever
*/
public class ThrowableSubject extends Subject {
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/com/google/common/truth/Truth.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ public static ClassSubject assertThat(@Nullable Class<?> actual) {
return assert_().that(actual);
}

/**
* Begins an assertion about a {@link Throwable}.
*
* <p>Truth does not provide its own support for calling a method and automatically catching an
* expected exception, only for asserting on the exception after it has been caught. To catch the
* exception, we suggest {@link org.junit.Assert#assertThrows(Class,
* org.junit.function.ThrowingRunnable) assertThrows} (JUnit), <a
* href="https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-fails-with.html">{@code
* assertFailsWith}</a> ({@code kotlin.test}), or similar functionality from your testing library
* of choice.
*
* <pre>
* InvocationTargetException expected =
* assertThrows(InvocationTargetException.class, () -> method.invoke(null));
* assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
* </pre>
*/
public static ThrowableSubject assertThat(@Nullable Throwable actual) {
return assert_().that(actual);
}
Expand Down